From ea776334b32cbb37a07eb16ac937b3f51ba3c77f Mon Sep 17 00:00:00 2001 From: Daniel Borges Date: Mon, 7 Jan 2019 22:21:44 +0100 Subject: [PATCH] useful macros, jingle when toggling edit mode, sound effect when clearing --- Code/edit.asm | 127 +++++++++++++++++++++++++++++++++--------- Code/nintendo-out.asm | 15 ++--- Code/render.asm | 4 +- Code/utils.inc | 20 ++++--- rom.gb | Bin 32768 -> 32768 bytes 5 files changed, 119 insertions(+), 47 deletions(-) diff --git a/Code/edit.asm b/Code/edit.asm index 7915990..ff32daf 100644 --- a/Code/edit.asm +++ b/Code/edit.asm @@ -5,7 +5,7 @@ SPRITE_ANIM_DELAY EQU 12 REPEAT_START_DELAY EQU 16 REPEAT_DELAY EQU 3 -Section "Edit memory", HRAM +SECTION "Edit memory", HRAM SelectX: ds 1 SelectY: ds 1 Down: ds 1 @@ -26,22 +26,64 @@ InitEdit: ldh [RepeatDelay], a ret +SECTION "Jingle", ROM0 +Jingle: + + ; load initial frequency into HL +FREQUENCY = 330 + ld hl, PULSE_FREQUENCY + + ; load step to be added to frequency in DE + ; based on if a != 0 or not + or a + jr z, .up +.down + ld de, 100 + jr .do +.up + ld de, -100 + +.do + ; load note count + ld b, 3 +.loop + + ; play pulse channel 1 with frequency set in HL + xor a + ldh [rNR10], a ; sweep + ld a, (%01 << 6) + 30 + ldh [rNR11], a ; pattern + sound length + ld a, $43 + ldh [rNR12], a ; init volume + envelope sweep + ld a, l + ldh [rNR13], a + ld a, h + or a, SOUND_START + ldh [rNR14], a + + ; add DE to HL frequency + add hl, de + + ; wait ~166ms + ld c, 6 +.delay + HaltAndClearInterrupts + dec c + jr nz, .delay + + ; repeat a few times + dec b + ret z + jr .loop + EXPORT EditOldBuffer SECTION "Edit old buffer", ROM0 EditOldBuffer: ; check start has been pressed ldh a, [JoypadDown] - and a, JOYPAD_START + and a, PADF_START ret z - ; wait v-blank - halt - - ; show sprites - ldh a, [rLCDC] - or a, LCDCF_OBJON - ldh [rLCDC], a - ; sound ON ld a, $80 ldh [rNR52], a @@ -49,6 +91,18 @@ EditOldBuffer: ldh [rNR50], a ; max volume on both speakers ld a, $99 ldh [rNR51], a ; channels 1 (pulse) and 4 (noise) on both speakers + + ; play jingle with notes going down + xor a + call Jingle + + ; wait v-blank + halt + + ; show sprites + ldh a, [rLCDC] + or a, LCDCF_OBJON + ldh [rLCDC], a ; init sprite animation xor a @@ -60,12 +114,12 @@ EditOldBuffer: ldh a, [Video + 1] xor a, %100 ldh [Video + 1], a - -.loop + ; clear interrupts xor a ldh [rIF], a +.loop ; compute cursor X position ldh a, [SelectX] sla a @@ -102,16 +156,16 @@ EditOldBuffer: ; check A button has been pressed ldh a, [JoypadDown] - and a, JOYPAD_A + and a, PADF_A call nz, ToggleCell ldh a, [JoypadDown] - and a, JOYPAD_SELECT + and a, PADF_SELECT call nz, Clear ; check start has been pressed ldh a, [JoypadDown] - and a, JOYPAD_START + and a, PADF_START jr nz, .exit ; reset input @@ -155,7 +209,7 @@ EditOldBuffer: .do ; check left direction ldh a, [Down] - and a, JOYPAD_LEFT + and a, PADF_LEFT jr z, .endLeft ldh a, [SelectX] and a @@ -166,7 +220,7 @@ EditOldBuffer: ; check up direction ldh a, [Down] - and a, JOYPAD_UP + and a, PADF_UP jr z, .endUp ldh a, [SelectY] and a @@ -177,7 +231,7 @@ EditOldBuffer: ; check right direction ldh a, [Down] - and a, JOYPAD_RIGHT + and a, PADF_RIGHT jr z, .endRight ldh a, [SelectX] cp a, 39 @@ -188,7 +242,7 @@ EditOldBuffer: ; check down direction ldh a, [Down] - and a, JOYPAD_DOWN + and a, PADF_DOWN jr z, .endDown ldh a, [SelectY] cp a, 35 @@ -199,12 +253,15 @@ EditOldBuffer: ; wait v-blank .skip - halt + HaltAndClearInterrupts jp .loop .exit + ld a, 1 + call Jingle + ; hide sprites ldh a, [rLCDC] and a, ~LCDCF_OBJON @@ -215,13 +272,20 @@ EditOldBuffer: xor a, %100 ldh [Video + 1], a + ; wait for sound to finish + ld b, 10 +.waitSound + HaltAndClearInterrupts + dec b + jr nz, .waitSound + ; sound OFF xor a ldh [rNR52], a ret -Section "Value to flag", ROM0, ALIGN[8] +SECTION "Value to flag", ROM0, ALIGN[8] Flag: db 1, 2, 4, 8 ; \1: horizontal stride @@ -245,6 +309,7 @@ MoveToCell: MACRO add hl, de ENDM +SECTION "Toggle cell", ROM0 ToggleCell: ; compute cell number in 2x2 cell group ldh a, [SelectX] @@ -309,14 +374,16 @@ ToggleCell: ldh [rNR11], a ; pattern + sound length ld a, $43 ldh [rNR12], a ; init volume + envelope sweep - ld a, $D7 - ldh [rNR13], a ; frequency low ($6D7 = 1751 => 440Hz) - ld a, $83 - ldh [rNR14], a ; start + frequency high +FREQUENCY = 100 + ld a, LOW(PULSE_FREQUENCY) + ldh [rNR13], a + ld a, SOUND_START | HIGH(PULSE_FREQUENCY) + ldh [rNR14], a .exit ret +SECTION "Clear buffers", ROM0 Clear: ; load old buffer address and store into rendered ldh a, [Progress] @@ -331,6 +398,16 @@ Clear: ld d, 0 call MemorySet + ; play long noise + xor a + ldh [rNR41], a ; set sound duration + ld a, $F4 + ldh [rNR42], a ; set volume with long sweep + ld a, $72 + ldh [rNR43], a ; set frequency + ld a, $80 + ldh [rNR44], a ; start + ; render cleared buffer call StartRender call WaitRender diff --git a/Code/nintendo-out.asm b/Code/nintendo-out.asm index 40df756..f7e0de9 100644 --- a/Code/nintendo-out.asm +++ b/Code/nintendo-out.asm @@ -1,16 +1,11 @@ INCLUDE "hardware.inc" +INCLUDE "utils.inc" SECTION "Random", ROM0, ALIGN[4] Random: db -1, 1, 2, -1, 4, 3, -2, -1, -4, 3, -1, 5, 2 RandomEnd: -HaltAndClearIF: MACRO - halt - xor a - ld [rIF], a -ENDM - EXPORT ScrollNintendoOut SECTION "Scroll Nintendo Out", ROM0 ScrollNintendoOut: @@ -18,7 +13,7 @@ ScrollNintendoOut: ; wait a moment ld b, 32 .wait - HaltAndClearIF + HaltAndClearInterrupts dec b jr nz, .wait @@ -45,8 +40,8 @@ ScrollNintendoOut: ld e, 0 ld b, 42 .noise - HaltAndClearIF - HaltAndClearIF + HaltAndClearInterrupts + HaltAndClearInterrupts ld a, [de] ldh [rSCY], a @@ -85,7 +80,7 @@ ScrollNintendoOut: ; nintendo logo lift-off! .scrollup - HaltAndClearIF + HaltAndClearInterrupts ; scroll up ldh a, [rSCY] diff --git a/Code/render.asm b/Code/render.asm index a02fba7..1c82869 100644 --- a/Code/render.asm +++ b/Code/render.asm @@ -149,9 +149,7 @@ IF RENDER_IN_HBL != 0 ; enable only v-blank interrupt and wait for vbl ld a, IEF_VBLANK ld [rIE], a - xor a - ldh [rIF], a - halt + HaltAndClearInterrupts ENDC ret \ No newline at end of file diff --git a/Code/utils.inc b/Code/utils.inc index c38d376..70beff6 100644 --- a/Code/utils.inc +++ b/Code/utils.inc @@ -1,14 +1,9 @@ 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 -JOYPAD_DIRECTIONS EQU $F0 +JOYPAD_DIRECTIONS EQU (PADF_UP | PADF_DOWN | PADF_LEFT | PADF_RIGHT) + +SOUND_START EQU $80 +PULSE_FREQUENCY EQUS "(-131072 / (FREQUENCY) + 2048)" ; destroys A ClearAndEnableInterrupts: MACRO @@ -17,6 +12,13 @@ ClearAndEnableInterrupts: MACRO ldh [rIF], a ENDM + ; destroys A +HaltAndClearInterrupts: MACRO + xor a + ldh [rIF], a + halt +ENDM + ; \1: sprite ID ; \2: X position ; \3: Y position diff --git a/rom.gb b/rom.gb index 4951e14ea6e102c99a0f45ebeb543af607ead1b6..78eddb556cc1382a6ad414e53fef462ed5f51ca4 100644 GIT binary patch delta 530 zcmYk1PiPZC6vk&}|FDfsb|(!XMTOm1YiTNo2YZoWO_T~kg?dnW@Z`Ts8>-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