diff --git a/Code/automata.asm b/Code/automata.asm index cbd8180..a45ee28 100644 --- a/Code/automata.asm +++ b/Code/automata.asm @@ -15,7 +15,7 @@ EXPORT Buffer1 SECTION "Automata buffer 1", WRAM0, ALIGN[9] Buffer1: ds 20 * 18 -EXPORT Old +EXPORT New, Old, Progress SECTION "Automata data", HRAM New: ds 1 ; high byte of pointer to bufferX Old: ds 1 ; high byte of pointer to bufferX diff --git a/Code/edit.asm b/Code/edit.asm index db04293..1f194c6 100644 --- a/Code/edit.asm +++ b/Code/edit.asm @@ -1,8 +1,13 @@ INCLUDE "hardware.inc" +INCLUDE "utils.inc" + +SPRITE_ANIM_DELAY EQU 12 Section "Edit memory", HRAM SelectX: ds 1 SelectY: ds 1 +SpriteAnimation: ds 1 +SpriteDelay: ds 1 EXPORT InitEdit SECTION "Init edit", ROM0 @@ -15,23 +20,186 @@ InitEdit: EXPORT EditOldBuffer SECTION "Edit old buffer", ROM0 -EditOldBuffer: +EditOldBuffer: + ; check start has been pressed ldh a, [JoypadDown] - and a, %1000 + and a, JOYPAD_START ret z -.loop + ; wait v-blank halt + + ; show sprites + ldh a, [rLCDC] + or a, LCDCF_OBJON + ldh [rLCDC], a + + ; init sprite animation xor a - ldh a, [rIF] + ldh [SpriteAnimation], a + ld a, SPRITE_ANIM_DELAY + ldh [SpriteDelay], a + +.loop + ; clear interrupts + xor a + ldh [rIF], a + + ; compute cursor X position + ldh a, [SelectX] + sla a + sla a + add a, 8 + ld b, a + + ; compute cursor Y position + ldh a, [SelectY] + sla a + sla a + add a, 16 + ld c, a + + ; update and load sprite animation + ldh a, [SpriteDelay] + dec a + ldh [SpriteDelay], a + jr nz, .same + ldh a, [SpriteAnimation] + inc a + and a, 3 + ldh [SpriteAnimation], a + ld a, SPRITE_ANIM_DELAY + ldh [SpriteDelay], a +.same + ldh a, [SpriteAnimation] + ld d, a + + ; update sprite in OAM + SetSprite 0, b, c, d, 0 call UpdateJoypad + + ; check A button has been pressed ldh a, [JoypadDown] - and a, %1000 - ret nz + and a, JOYPAD_A + call nz, ToggleCell - jr .loop + ; check start has been pressed + ldh a, [JoypadDown] + and a, JOYPAD_START + jr nz, .exit + ; check left direction + ldh a, [JoypadDown] + and a, JOYPAD_LEFT + jr z, .endLeft + ldh a, [SelectX] + and a + jr z, .endLeft + dec a + ldh [SelectX], a +.endLeft + ; check up direction + ldh a, [JoypadDown] + and a, JOYPAD_UP + jr z, .endUp + ldh a, [SelectY] + and a + jr z, .endUp + dec a + ldh [SelectY], a +.endUp - \ No newline at end of file + ; check right direction + ldh a, [JoypadDown] + and a, JOYPAD_RIGHT + jr z, .endRight + ldh a, [SelectX] + cp a, 39 + jr nc, .endRight + inc a + ldh [SelectX], a +.endRight + + ; check down direction + ldh a, [JoypadDown] + and a, JOYPAD_DOWN + jr z, .endDown + ldh a, [SelectY] + cp a, 35 + jr nc, .endDown + inc a + ldh [SelectY], a +.endDown + + ; wait v-blank + halt + + jp .loop + +.exit + + ; hide sprites + ldh a, [rLCDC] + and a, ~LCDCF_OBJON + ldh [rLCDC], a + + ret + +Section "Value to flag", ROM0, ALIGN[8] +Flag: db 1, 2, 4, 8 + + ; \1: horizontal stride +ToggleInTargetBuffer: MACRO + ; move pointer to 2x2 cell group + ldh a, [SelectY] + sra a + jr z, .addX\@ + ld c, a + ld de, \1 +.mul\@ + add hl, de + dec c + jr nz, .mul\@ +.addX\@ + xor a + ld d, a + ldh a, [SelectX] + sra a + ld e, a + add hl, de + + ; compute cell number in 2x2 cell group + ldh a, [SelectX] + and a, 1 + ld b, a + ldh a, [SelectY] + and a, 1 + sla a + or a, b + + ; transform cell number to bit offset in 2x2 cell + ld d, HIGH(Flag) + ld e, a + ld a, [de] + ld b, a + + ld a, [hl] + xor a, b + ld [hl], a +ENDM + +ToggleCell: + ldh a, [Video] + ld l, a + ldh a, [Video + 1] + xor a, %100 ; change to displayed video buffer + ld h, a + ToggleInTargetBuffer 32 + ldh a, [Progress] + ld l, a + ldh a, [Old] + ld h, a + ToggleInTargetBuffer 20 + ret \ No newline at end of file diff --git a/Code/main.asm b/Code/main.asm index bd68ee7..eb17db4 100644 --- a/Code/main.asm +++ b/Code/main.asm @@ -1,4 +1,5 @@ INCLUDE "hardware.inc" +INCLUDE "utils.inc" EMPTY_BG_TILE EQU 17 _VRAM_BG_TILES EQU $9000 @@ -82,11 +83,12 @@ ENDC ld a, LCDCF_ON ld [rLCDC], a + ClearAndEnableInterrupts .mainloop call StartRender call UpdateAutomata call WaitRender + call SwapBuffers call UpdateJoypad call EditOldBuffer - call SwapBuffers jp .mainloop diff --git a/Code/render.asm b/Code/render.asm index ab6f75a..1418f6f 100644 --- a/Code/render.asm +++ b/Code/render.asm @@ -132,7 +132,6 @@ ELSE ld a, IEF_VBLANK ENDC ld [rIE], a - ClearAndEnableInterrupts ret @@ -141,11 +140,11 @@ SECTION "WaitRender", ROM0 WaitRender: ldh a, [LinesLeft] or a - ret z + jr z, .exit halt jr WaitRender - di +.exit IF RENDER_IN_HBL != 0 ; enable only v-blank interrupt and wait for vbl ld a, IEF_VBLANK @@ -155,4 +154,16 @@ IF RENDER_IN_HBL != 0 halt ENDC + ; move video pointer back to beginning + ld hl, Video + ld a, [hl+] + ld h, [hl] + ld l, a + ld de, -(32*18) + add hl, de + ld a, l + ldh [Video], a + ld a, h + ldh [Video + 1], a + ret \ No newline at end of file diff --git a/Code/utils.inc b/Code/utils.inc index 63b6586..2f4a054 100644 --- a/Code/utils.inc +++ b/Code/utils.inc @@ -1,10 +1,19 @@ INCLUDE "hardware.inc" +JOYPAD_A EQU $01 +JOYPAD_B EQU $02 +JOYPAD_SELECT EQU $04 +JOYPAD_START EQU $08 +JOYPAD_RIGHT EQU $10 +JOYPAD_LEFT EQU $20 +JOYPAD_UP EQU $40 +JOYPAD_DOWN EQU $80 + ; destroys A ClearAndEnableInterrupts: MACRO xor a ei ; will take effect AFTER next instruction - ldh a, [rIF] + ldh [rIF], a ENDM ; \1: sprite ID diff --git a/Graphics/BackgroundTiles.gbr b/Graphics/BackgroundTiles.gbr index 4a8c2e5..34a1eda 100644 Binary files a/Graphics/BackgroundTiles.gbr and b/Graphics/BackgroundTiles.gbr differ diff --git a/Graphics/SpriteTiles.bin b/Graphics/SpriteTiles.bin index 43d4178..d4e61b6 100644 Binary files a/Graphics/SpriteTiles.bin and b/Graphics/SpriteTiles.bin differ diff --git a/Graphics/SpriteTiles.gbr b/Graphics/SpriteTiles.gbr index fe6258a..d38a6a3 100644 Binary files a/Graphics/SpriteTiles.gbr and b/Graphics/SpriteTiles.gbr differ diff --git a/rom.gb b/rom.gb index bac0aef..0194acf 100644 Binary files a/rom.gb and b/rom.gb differ