collapseos/kernel
Clanmaster21 cca3157c66 addHL and subHL affect flags, and are smaller (#30)
* addHL and subHL affect flags, and are smaller

Most importantly, addHL and subHL now affect the flags as you would expect from a 16 bit addition/subtraction. This seems like it'd be preferred behaviour, however I realise any code relying on it not affecting flags would break. One byte saved in addHL, and two bytes saved in subHL. Due to the branching nature of the original code, it's difficult to compare speeds, subHL is either 1 or 6 cycles faster depending on branching, and addHL is between -1 and 3 cycles faster. If the chance of a carry is 50%, addHL is expected to be a cycle faster, but for a chance of carry below 25% (so a < 0x40) this will be up to a cycle slower.

* Update core.asm

* Reworked one use of addHL

By essentially inlining both addHL and cpHLDE, 100 cycles are saved, but due to the registers not needing preserving, a byte is saved too.

* Corrected spelling error in comment

* Reworked second use of addHL

43 cycles saved, and no more addHL in critical loops. No bytes saved or used.

* Fixed tabs and spacing, and made a comment clearer.

* Clearer comments

* Adopted push/pop notation
2019-10-17 16:45:27 -04:00
..
sms sms/vdp: clear 2 lines forward when doing LF 2019-07-22 11:03:44 -04:00
acia.asm acia: protect DE during aciaInt 2019-06-16 19:29:58 -04:00
blockdev_cmds.asm blockdev: fix bug recently introduced in load cmd 2019-06-17 09:54:30 -04:00
blockdev.asm blockdev: protect IX in routines 2019-07-14 12:17:13 -04:00
core.asm addHL and subHL affect flags, and are smaller (#30) 2019-10-17 16:45:27 -04:00
err.h blockdev: make implementors "random access" 2019-06-04 15:36:20 -04:00
fs_cmds.asm fs: fix broken fopn on id > 0 2019-06-05 15:40:56 -04:00
fs.asm Fix misc. source comment typos 2019-10-09 11:12:08 -04:00
kbd.asm kbd: add keypad codes to keycode table 2019-07-25 14:22:17 -04:00
mmap.asm mmap: add MMAP_LEN parameter 2019-07-23 23:00:32 -04:00
parse.asm parse: fix option word default value 2019-06-14 21:10:18 -04:00
pgm.asm shell: allow cmds to be shorter than 4 chars 2019-07-21 15:57:55 -04:00
README.md ed: add 'd' cmd 2019-07-14 10:32:28 -04:00
sdc.asm sdc: make sdcReadBlk return error on max retries 2019-06-19 13:22:07 -04:00
shell.asm Fix misc. source comment typos 2019-10-09 11:12:08 -04:00
stdio.asm stdio: fix broken ReadC logic 2019-07-19 14:44:47 -04:00
user.h.example zasm emul: bring back kernel/user distinction 2019-05-19 12:57:59 -04:00

Kernel

Bits and pieces of code that you can assemble to build a kernel for your machine.

These parts are made to be glued together in a single glue.asm file you write yourself.

This code is designed to be assembled by Collapse OS' own [zasm][zasm].

Defines

Each part can have its own constants, but some constant are made to be defined externally. We already have some of those external definitions in platform includes, but we can have more defines than this.

Each part has a "DEFINES" section listing the constant it expects to be defined. Make sure that you have these constants defined before you include the file.

Variable management

Each part can define variables. These variables are defined as addresses in RAM. We know where RAM start from the RAMSTART constant in platform includes, but because those parts are made to be glued together in no pre-defined order, we need a system to align variables from different modules in RAM.

This is why each part that has variable expect a <PARTNAME>_RAMSTART constant to be defined and, in turn, defines a <PARTNAME>_RAMEND constant to carry to the following part.

Thus, code that glue parts together could look like:

MOD1_RAMSTART .equ RAMSTART
#include "mod1.asm"
MOD2_RAMSTART .equ MOD1_RAMEND
#include "mod2.asm"

Stack management

Keeping the stack "balanced" is a big challenge when writing assembler code. Those push and pop need to correspond, otherwise we end up with completely broken code.

The usual "push/pop" at the beginning and end of a routine is rather easy to manage, nothing special about them.

The problem is for the "inner" push and pop, which are often necessary in routines handling more data at once. In those cases, we walk on eggshells.

A naive approach could be to indent the code between those push/pop, but indent level would quickly become too big to fit in 80 chars.

I've tried ASCII art in some places, where comments next to push/pop have "|" indicating the scope of the push/pop. It's nice, but it makes code complicated to edit, especially when dense comments are involved. The pipes have to go through them.

Of course, one could add descriptions next to each push/pop describing what is being pushed, and I do it in some places, but it doesn't help much in easily tracking down stack levels.

So, what I've started doing is to accompany each "non-routine" (at the beginning and end of a routine) push/pop with "--> lvl X" and "<-- lvl X" comments. Example:

push    af  ; --> lvl 1
inc     a
push    af  ; --> lvl 2
inc     a
pop     af  ; <-- lvl 2
pop     af  ; <-- lvl 1

I think that this should do the trick, so I'll do this consistently from now on. [zasm]: ../apps/zasm/README.md