added interrupt handlers for rendering (no rendering yet)

This commit is contained in:
2018-12-27 19:39:42 +01:00
parent 0ea98b1904
commit 23b6ad662f

View File

@@ -15,14 +15,19 @@ MemoryCopy:
or c or c
jr nz, MemoryCopy jr nz, MemoryCopy
ret ret
SECTION "V-Blank Interrupt Handler", ROM0[$40] SECTION "Memory Set", ROM0
VBlankInterruptHandler: ; hl = destination
reti ; d = data
; bc = count
SECTION "LCD Stat Interrupt Handler", ROM0[$48] MemorySet:
LCDStatInterruptHandler: ld a, d
reti ld [hl+], a
dec bc
ld a, b
or c
jr nz, MemorySet
ret
SECTION "Timer Interrupt Handler", ROM0[$50] SECTION "Timer Interrupt Handler", ROM0[$50]
TimerInterruptHandler: TimerInterruptHandler:
@@ -46,6 +51,9 @@ ENDR
SECTION "Main", ROM0[$150] SECTION "Main", ROM0[$150]
Start: Start:
; shut sound off
ld [rNR52], a
; enable v-blank interrupt ; enable v-blank interrupt
ld a, IEF_VBLANK ld a, IEF_VBLANK
ld [rIE], a ld [rIE], a
@@ -53,10 +61,7 @@ Start:
; enable interrupts ; enable interrupts
ei ei
; shut sound off ; wait for v-blank
ld [rNR52], a
; wait for VBL
halt halt
; disable screen ; disable screen
@@ -67,27 +72,26 @@ Start:
ld a, %11100100 ld a, %11100100
ld [rBGP], a ld [rBGP], a
; load tiles ; load 18 tiles
; 0..15: 2x2 cell combinations
; 16: sprite selection tile
; 17: empty tile
ld hl, _VRAM_BG_TILES ld hl, _VRAM_BG_TILES
ld de, Tiles ld de, Tiles
ld bc, 16 ld bc, TilesEnd - Tiles
call MemoryCopy
ld hl, _VRAM_BG_TILES + 16
ld de, Tiles + 16
ld bc, 16
call MemoryCopy call MemoryCopy
; set scrolling to (0, 0) ; set scrolling to (32, 16)
xor a ld a, 32
ld [rSCY], a
ld [rSCX], a ld [rSCX], a
ld a, 16
ld [rSCY], a
; init ram ; clear screen (both buffers)
ld hl, _SCRN0 ld hl, _SCRN0
ld de, DefaultMap ld d, 17 ; empty tile
ld bc, 32 * 32 ld bc, 32 * 32 * 2
call MemoryCopy call MemorySet
; init buffer 0 ; init buffer 0
ld hl, Buffer0 ld hl, Buffer0
@@ -105,28 +109,38 @@ Start:
; set old pointer to buffer0 ; set old pointer to buffer0
ld a, HIGH(Buffer0) ld a, HIGH(Buffer0)
ld [OldPointer + 1], a ld [Old + 1], a
; set new pointer to buffer1 ; set new pointer to buffer1
ld a, HIGH(Buffer1) ld a, HIGH(Buffer1)
ld [NewPointer + 1], a ld [New + 1], a
; shet video pointer to second tilemap
ld a, HIGH(_SCRN1)
ldh [Video + 1], a
; set low byte of pointers to 0 (start of buffer is aligned) ; set low byte of pointers to 0 (start of buffer is aligned)
xor a xor a
ld [OldPointer + 0], a ld [Old + 0], a
ld [NewPointer + 0], a ld [New + 0], a
.mainloop .mainloop
; enable v-blank and lcd stat interrupt for h-blank
di
ld a, IEF_VBLANK | IEF_LCDC
ld [rIE], a
ei
.topleft .topleft
; handle top left corner ; handle top left corner
ld bc, TopLeftCorner ld bc, TopLeftCorner
call Conway call Conway
; advance to next cell in top row ; advance to next cell in top row
ld hl, NewPointer ld hl, New
inc [hl] inc [hl]
ld hl, OldPointer ld hl, Old
inc [hl] inc [hl]
; handle all cells in top row except corners ; handle all cells in top row except corners
@@ -139,9 +153,9 @@ Start:
call Conway call Conway
; advance to next cell in top row ; advance to next cell in top row
ld hl, NewPointer ld hl, New
inc [hl] inc [hl]
ld hl, OldPointer ld hl, Old
inc [hl] inc [hl]
; decrement x loop ; decrement x loop
@@ -155,9 +169,9 @@ Start:
call Conway call Conway
; advance pointers to next row ; advance pointers to next row
ld hl, NewPointer ld hl, New
inc [hl] inc [hl]
ld hl, OldPointer ld hl, Old
inc [hl] inc [hl]
ld a, 30 ld a, 30
@@ -169,9 +183,9 @@ Start:
call Conway call Conway
; advance to next cell ; advance to next cell
ld hl, NewPointer ld hl, New
inc [hl] inc [hl]
ld hl, OldPointer ld hl, Old
inc [hl] inc [hl]
ld a, 30 ld a, 30
@@ -183,9 +197,9 @@ Start:
call Conway call Conway
; advance to next cell ; advance to next cell
ld hl, NewPointer ld hl, New
inc [hl] inc [hl]
ld hl, OldPointer ld hl, Old
inc [hl] inc [hl]
; decrement x loop ; decrement x loop
@@ -199,14 +213,14 @@ Start:
call Conway call Conway
; advance to next row ; advance to next row
ld hl, NewPointer ld hl, New
inc [hl] inc [hl]
ld hl, OldPointer ld hl, Old
inc [hl] inc [hl]
jr nz, .nocarry jr nz, .nocarry
ld hl, NewPointer + 1 ld hl, New + 1
inc [hl] inc [hl]
ld hl, OldPointer + 1 ld hl, Old + 1
inc [hl] inc [hl]
.nocarry .nocarry
@@ -221,9 +235,9 @@ Start:
call Conway call Conway
; advance to next cell in bottom row ; advance to next cell in bottom row
ld hl, NewPointer ld hl, New
inc [hl] inc [hl]
ld hl, OldPointer ld hl, Old
inc [hl] inc [hl]
; handle all cells in bottom row except corners ; handle all cells in bottom row except corners
@@ -236,9 +250,9 @@ Start:
call Conway call Conway
; advance to next cell in top row ; advance to next cell in top row
ld hl, NewPointer ld hl, New
inc [hl] inc [hl]
ld hl, OldPointer ld hl, Old
inc [hl] inc [hl]
; decrement x loop ; decrement x loop
@@ -251,28 +265,38 @@ Start:
ld bc, BottomRightCorner ld bc, BottomRightCorner
call Conway call Conway
; wait for v-blank to swap pointers and render new buffer ; enable only v-blank interrupt
di
ld a, IEF_VBLANK
ld [rIE], a
ei
; wait v-blank
halt halt
; swap buffers and reset pointers ; swap pointers and display bg that has just been filled
ld a, [NewPointer + 1] ld a, [New + 1]
cp a, HIGH(Buffer1) cp a, HIGH(Buffer1)
jr c, .newToBuffer0 jr c, .newToBuffer1
ld a, HIGH(Buffer0) ld a, HIGH(Buffer0)
ld [NewPointer + 1], a ld [New + 1], a
ld a, HIGH(Buffer1) ld a, HIGH(Buffer1)
ld [OldPointer + 1], a ld [Old + 1], a
ld a, HIGH(_SCRN0)
ldh [Video + 1], a
; display bg 9C00 ; display bg 9C00
ld a, LCDCF_ON | LCDCF_BGON | LCDCF_BG9C00 ld a, LCDCF_ON | LCDCF_BGON | LCDCF_BG9C00
ld [rLCDC], a ld [rLCDC], a
jr .resetlow jr .resetlow
.newToBuffer0 .newToBuffer1
ld a, HIGH(Buffer1) ld a, HIGH(Buffer1)
ld [NewPointer + 1], a ld [New + 1], a
ld a, HIGH(Buffer0) ld a, HIGH(Buffer0)
ld [OldPointer + 1], a ld [Old + 1], a
ld a, HIGH(_SCRN1)
ldh [Video + 1], a
; display bg 9800 ; display bg 9800
ld a, LCDCF_ON | LCDCF_BGON | LCDCF_BG9800 ld a, LCDCF_ON | LCDCF_BGON | LCDCF_BG9800
@@ -281,8 +305,9 @@ Start:
.resetlow .resetlow
; reset low bytes of pointers ; reset low bytes of pointers
xor a xor a
ld [NewPointer], a ld [New], a
ld [OldPointer], a ld [Old], a
ldh [Video], a
jp .mainloop jp .mainloop
@@ -312,7 +337,7 @@ Conway:
ld c, l ld c, l
; load old pointer ; load old pointer
ld hl, OldPointer ld hl, Old
ld a, [hl+] ld a, [hl+]
ld h, [hl] ld h, [hl]
ld l, a ld l, a
@@ -336,7 +361,7 @@ Conway:
.decide .decide
; load old pointer ; load old pointer
ld hl, OldPointer ld hl, Old
ld a, [hl+] ld a, [hl+]
ld h, [hl] ld h, [hl]
ld l, a ld l, a
@@ -358,7 +383,7 @@ Conway:
.writealive .writealive
; load new pointer ; load new pointer
ld hl, NewPointer ld hl, New
ld a, [hl+] ld a, [hl+]
ld h, [hl] ld h, [hl]
ld l, a ld l, a
@@ -383,7 +408,7 @@ Conway:
.writedead .writedead
; load new pointer ; load new pointer
ld hl, NewPointer ld hl, New
ld a, [hl+] ld a, [hl+]
ld h, [hl] ld h, [hl]
ld l, a ld l, a
@@ -393,29 +418,69 @@ Conway:
ld [hl], a ld [hl], a
ret ret
SECTION "V-Blank Interrupt Handler", ROM0[$40]
VBlankInterruptHandler:
; save A and flags
push af
; set max number of cells to render
ld a, 15
ldh [RenderCount], a
; render
jp Render
SECTION "LCD Stat Interrupt Handler", ROM0[$48]
LCDStatInterruptHandler:
; save A and flags
push af
; set max number of cells to render
ld a, 5
ldh [RenderCount], a
; render
jp Render
SECTION "Work", WRAM0[$C000] SECTION "Render", ROM0
Render:
; save DE, BC, HL registers
push de
push bc
push hl
.loop
ld hl, RenderCount
dec [hl]
jr nz, .loop
; restore DE, BC, HL registers
pop hl
pop bc
pop de
; restore A and flags, saved in interrupt handler
pop af
; return from v-blank or lcd interrupt
reti
SECTION "Update Memory", WRAM0[$C000]
Buffer0: ds 32 * 32 Buffer0: ds 32 * 32
Buffer1: ds 32 * 32 Buffer1: ds 32 * 32
OldPointer: ds 2 Old: ds 2
NewPointer: ds 2 New: ds 2
Alive: ds 1 Alive: ds 1
XLoop: ds 1 XLoop: ds 1
YLoop: ds 1 YLoop: ds 1
Tile: ds 1 Tile: ds 1
SECTION "Game of Life neighboring cells offset tables", ROM0 SECTION "Render Memory", HRAM
; for a looping grid of 20x18 cells, with stride 32 Video: ds 2
;TopLeftCorner: dw 1, 33, 32, 51, 19, 563, 544, 545, 0 RenderCount: ds 1
;TopRightCorner: dw -19, 13, 32, 31, -1, 543, 544, 525, 0
;BottomLeftCorner: dw 1, -543, -544, -525, 19, -13, -32, -31, 0
;BottomRightCorner: dw -19, -563, -544, -545, -1, -33, -32, -51, 0
;TopRow: dw 1, 33, 32, 31, -1, 543, 544, 545, 0
;BottomRow: dw 1, -543, -544, -545, -1, -33, -32, -31, 0
;LeftColumn: dw 1, 33, 32, 51, 19, -13, -32, -31, 0
;RightColumn: dw -19, 13, 32, 31, -1, -33, -32, -51, 0
;Inner: dw 1, 33, 32, 31, -1, -33, -32, -31, 0
SECTION "Game of Life neighboring cells offset tables", ROM0
; for a looping grid of 32x32 cells ; for a looping grid of 32x32 cells
TopLeftCorner: dw 1, 33, 32, 63, 31, 1023, 992, 993, 0 TopLeftCorner: dw 1, 33, 32, 63, 31, 1023, 992, 993, 0
TopRightCorner: dw -31, 1, 32, 31, -1, 991, 992, 961, 0 TopRightCorner: d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