1
0
Fork 0
2021-8bit-cpu-gameboy/code/ops.asm

260 lines
2.7 KiB
NASM

SECTION "Operations", ROM0
; For all instructions, b contains the current op stage
; [in] a: instruction
; [in] b: stage index
; 0b0000
op_hlt:
ld c, a
ld a, b
cp 2
jr nz, .exit
.stage_2:
ld hl, ctrl
set CTRL_HT, [hl]
.exit:
ld c, a
ret
; 0b0001
op_lda:
ld c, a
ld a, b
cp 2
jr z, .stage_2
cp 3
jr z, .stage_3
jr .exit
.stage_2:
ld hl, ctrl
set CTRL_MI, [hl]
set CTRL_IO, [hl]
jr .exit
.stage_3:
ld hl, ctrl
set CTRL_RO, [hl]
set CTRL_AI, [hl]
.exit:
ld c, a
ret
; 0b0010
op_add:
ld c, a
ld a, b
cp 2
jr z, .stage_2
cp 3
jr z, .stage_3
cp 4
jr z, .stage_4
jr .exit
.stage_2:
ld hl, ctrl
set CTRL_MI, [hl]
set CTRL_IO, [hl]
jr .exit
.stage_3:
ld hl, ctrl
set CTRL_RO, [hl]
inc hl
set CTRL_BI, [hl]
jr .exit
.stage_4:
ld hl, ctrl
set CTRL_AI, [hl]
inc hl
set CTRL_EO, [hl]
set CTRL_FI, [hl]
.exit:
ld c, a
ret
; 0b0011
op_sub:
ld c, a
ld a, b
cp 2
jr z, .stage_2
cp 3
jr z, .stage_3
cp 4
jr z, .stage_4
jr .exit
.stage_2:
ld hl, ctrl
set CTRL_MI, [hl]
set CTRL_IO, [hl]
jr .exit
.stage_3:
ld hl, ctrl
set CTRL_RO, [hl]
inc hl
set CTRL_BI, [hl]
jr .exit
.stage_4:
ld hl, ctrl
set CTRL_AI, [hl]
inc hl
set CTRL_EO, [hl]
set CTRL_SU, [hl]
set CTRL_FI, [hl]
.exit:
ld c, a
ret
; 0b0100
op_sta:
ld c, a
ld a, b
cp 2
jr z, .stage_2
cp 3
jr z, .stage_3
jr .exit
.stage_2:
ld hl, ctrl
set CTRL_MI, [hl]
set CTRL_IO, [hl]
jr .exit
.stage_3:
ld hl, ctrl
set CTRL_AO, [hl]
set CTRL_RI, [hl]
.exit:
ld c, a
ret
; 0b0101
op_ldi:
ld c, a
ld a, b
cp 2
jr nz, .exit
.stage_2:
ld hl, ctrl
set CTRL_IO, [hl]
set CTRL_AI, [hl]
.exit:
ld c, a
ret
; 0b0110
op_jmp:
ld c, a
ld a, b
cp 2
jr nz, .exit
.stage_2:
ld hl, ctrl
set CTRL_IO, [hl]
inc hl
set CTRL_JP, [hl]
.exit:
ld c, a
ret
; [in] a: instruction
; [in] b: stage index
; 0b0111
op_jc:
ld c, a ; save instruction
ld a, b
cp 2
jr nz, .exit
.stage_2:
; save stage index
ld d, a
ld hl, ctrl
set CTRL_IO, [hl]
inc hl
ld a, [flags]
bit FLAG_C, a
jr z, .exit
set CTRL_JP, [hl]
.exit:
; restore stage index
ld a, d
ld c, a
ret
; [in] a: instruction
; [in] b: stage index
; 0b1000
op_jz:
ld c, a ; save instruction
ld a, b
cp 2
jr nz, .exit
.stage_2:
; save stage index
ld d, a
ld hl, ctrl
set CTRL_IO, [hl]
inc hl
ld a, [flags]
bit FLAG_Z, a
jr z, .exit
set CTRL_JP, [hl]
.exit:
; restore stage index
ld a, d
ld c, a
ret
; 0b1111
op_out:
ld c, a
ld a, b
cp 2
jr nz, .exit
.stage_2:
ld hl, ctrl
set CTRL_AO, [hl]
inc hl
set CTRL_OI, [hl]
.exit:
ld c, a
ret