m16: Added zero flag. Moved m16 registers to hram.
authorLukas Krickl <lukas@krickl.dev>
Tue, 3 Feb 2026 06:26:48 +0000 (07:26 +0100)
committerLukas Krickl <lukas@krickl.dev>
Tue, 3 Feb 2026 06:26:48 +0000 (07:26 +0100)
src/defs.s
src/hram.s
src/math.s
src/wram.s

index 77b46a88f0780e2ae04eceacf68976f6e8aaff87..62fc779c6e7883a259879b6ab870f3b2a6553e52 100644 (file)
        ; 16 bit math flags
 .se 1
 .de M16_CARRY, 1
+.de M16_ZERO, 2
index 472ea3dec91a01e4a3f7eb878d7210da631b0327..7b81ba5d6b56398c2bdc88cd4748aec4c4da8822 100644 (file)
@@ -3,3 +3,10 @@
 ; 0x80 - 0x90 are used by dma routines
 .org 0xFF90
 
+       ; 16-bit math temporary registers
+       ; store in LE
+m16_a: .adv 2
+m16_b: .adv 2
+       ; flags for 16 bit math
+m16_flags: .adv 1
+
index 524586cbaeb434f9b1fb2a2aab4f7dcd255ec4fc..d1b0dcc5c500be4a07da4a3f8ac987033e1d49a1 100644 (file)
@@ -5,21 +5,21 @@
        ;               $1: m16_a/b
 #macro m16_write_bc
        ld a, c
-       ld [$1], a
+       ldh [$1], a
        ld a, b
-       ld [$1+1], a
+       ldh [$1+1], a
 #endmacro
 #macro m16_write_de
        ld a, e
-       ld [$1], a
+       ldh [$1], a
        ld a, d
-       ld [$1+1], a
+       ldh [$1+1], a
 #endmacro
 #macro m16_write_hl
        ld a, l
-       ld [$1], a
+       ldh [$1], a
        ld a, h
-       ld [$1+1], a
+       ldh [$1+1], a
 #endmacro
 
 
        ;       returns:
        ;               r16: 16 bit integer
 #macro m16_read_bc
-       ld a, [$1]
+       ldh a, [$1]
        ld c, a
-       ld a, [$1+1]
+       ldh a, [$1+1]
        ld b, a
 #endmacro
 #macro m16_read_de
-       ld a, [$1]
+       ldh a, [$1]
        ld e, a
-       ld a, [$1+1]
+       ldh a, [$1+1]
        ld d, a
 #endmacro
 #macro m16_read_hl
-       ld a, [$1]
+       ldh a, [$1]
        ld l, a
-       ld a, [$1+1]
+       ldh a, [$1+1]
        ld h, a
 #endmacro
 
+       ; sets the carry flag 
+       ; if required otehrwise jr @nc
+       ; inputs:
+       ;               c: carry flag
+#macro m16_set_carry
+.beginscope
+       ; no carry? good to exit
+       jr nc, @no_carry REL
+
+               ; when carry is set -> set flag
+               ld b, M16_CARRY
+               ldh a, [m16_flags]
+               or a, b
+               ldh [m16_flags], a
+@no_carry:
+.endscope
+#endmacro
+       
+       ; sets the zero flag
+       ; checks m16_a == 0
+       ; if so -> set zero flag
+       ; otherwise skip
+#macro m16_set_zero
+.beginscope
+       ldh a, [m16_a]
+       ld b, a
+       ldh a, [m16_a+1]
+       or a, b
+       jr nz, @not_zero REL
+               ld b, M16_ZERO
+               ldh a, [m16_flags]
+               or a, b
+               ldh [m16_flags], a
+@not_zero:
+.endscope
+#endmacro
+
        ; 16 bit add
        ; inputs:
        ;               m16_a/m16_b
        ;               m16_flags
 m16_add:
        xor a, a
-       ld [m16_flags], a
+       ldh [m16_flags], a
 
-       ld a, [m16_b]
+       ldh a, [m16_b]
        ld b, a
-       ld a, [m16_a]
+       ldh a, [m16_a]
        
        ; low is stored
        add a, b
-       ld [m16_a], a
+       ldh [m16_a], a
 
-       ld a, [m16_b+1]
+       ldh a, [m16_b+1]
        ld b, a
-       ld a, [m16_a+1]
+       ldh a, [m16_a+1]
 
        ; high + carry stored
        adc a, b
-       ld [m16_a+1], a
-       ret nc ; no carry? good to exit
+       ldh [m16_a+1], a
 
-       ; when carry is set -> set flag
-       ld b, M16_CARRY
-       ld a, [m16_flags]
-       or a, b
-       ld [m16_flags], a
+       m16_set_carry
+       m16_set_zero
 
        ret
 
@@ -91,29 +124,25 @@ m16_add:
        ;               m16_flags
 m16_sub:
        xor a, a
-       ld [m16_flags], a
+       ldh [m16_flags], a
 
-       ld a, [m16_b]
+       ldh a, [m16_b]
        ld b, a
-       ld a, [m16_a]
+       ldh a, [m16_a]
        
        ; low is stored
        sub a, b
-       ld [m16_a], a
+       ldh [m16_a], a
 
-       ld a, [m16_b+1]
+       ldh a, [m16_b+1]
        ld b, a
-       ld a, [m16_a+1]
+       ldh a, [m16_a+1]
 
        ; high + carry stored
        sbc a, b
-       ld [m16_a+1], a
-       ret nc
-
-       ; when carry is set -> set flag
-       ld b, M16_CARRY
-       ld a, [m16_flags]
-       or a, b
-       ld [m16_flags], a
+       ldh [m16_a+1], a
+       
+       m16_set_carry
+       m16_set_zero
 
        ret
index 9099c76dd49073e7f3ef8e294dca4207fadc2348..ddbb355a5a971ecf700bfb8c91a9458351bd005d 100644 (file)
@@ -142,12 +142,6 @@ col_tile: .adv 2
 col_y: .adv 1
 col_x: .adv 1
 
-       ; 16-bit math temporary registers
-       ; store in LE
-m16_a: .adv 2
-m16_b: .adv 2
-       ; flags for 16 bit math
-m16_flags: .adv 1
 
 ticks: .adv 1