Implement RNG

This commit is contained in:
Randy Thiemann 2023-10-13 08:38:10 +02:00
parent 6f0d5d539c
commit 9cedde3ed7
4 changed files with 215 additions and 92 deletions

View File

@ -1,8 +1,4 @@
; ***************************************************************************** SECTION "Cartridge Header", ROM0[$100]
; * * nop
; * Cartridge Header. * jp Main
; * * ds $150 - @, 0
; *****************************************************************************
SECTION "Header", ROM0[$100]
jp MainEntryPoint
ds $150 - @, 0 ; RGBFIX will fill in the header.

View File

@ -1,5 +1,107 @@
IF !DEF(RNG_ASM)
DEF RNG_ASM EQU 1
INCLUDE "globals.asm"
SECTION "RNG Variables", WRAM0
wRNGSeed: ds 4
wPieceHistory: ds 4
wNextPiece:: ds 1
section "RNG Functions", ROM0 section "RNG Functions", ROM0
NextByte:: StartNewGame::
; Do some bit fuckery on the seed using the gameboy's free-running timers.
ld hl, wRNGSeed
ldh a, [rDIV]
xor a, [hl]
ld [hl+], a
ldh a, [rTIMA]
xor a, [hl]
ld [hl+], a
ldh a, [rDIV]
xor a, [hl]
ld [hl+], a
ldh a, [rTIMA]
xor a, [hl]
ld [hl], a
; Initialize the next history.
ld hl, wPieceHistory
ld a, PIECE_Z
ld [hl+], a
ld [hl+], a
ld a, PIECE_S
ld [hl+], a
ld [hl], a
; Get the first piece and make sure it's not Z, S or O.
: call NextPiece
cp a, PIECE_Z
jr z, :-
cp a, PIECE_S
jr z, :-
cp a, PIECE_O
jr z, :-
; Store it.
ld hl, wPieceHistory
ld [hl], a
ld hl, wNextPiece
ld [hl], a
ret
GetNextPiece::
ld e, 7
: dec e
jr z, :+
call NextPiece
ld hl, wPieceHistory
cp a, [hl]
jr z, :-
inc hl
cp a, [hl]
jr z, :-
inc hl
cp a, [hl]
jr z, :-
inc hl
cp a, [hl]
jr z, :-
: ld hl, wNextPiece
ld [hl], a
ld b, a
ld hl, wPieceHistory+2
ld a, [hl+]
ld [hl], a
ld hl, wPieceHistory+1
ld a, [hl+]
ld [hl], a
ld hl, wPieceHistory
ld a, [hl+]
ld [hl-], a
ld a, b
ld [hl], a
ret
NextPiece:
call NextByte
and a, $07
cp a, 7
ret nz
dec a
ret
NextByte:
; Load seed ; Load seed
ld hl,wRNGSeed+3 ld hl,wRNGSeed+3
ld a, [hl-] ld a, [hl-]
@ -30,5 +132,7 @@ NextByte::
ld a, b ld a, b
adc $31 adc $31
ld [hl], a ld [hl], a
ld b,a
ret ret
ENDC

View File

@ -1,18 +1,21 @@
INCLUDE "globals.asm" INCLUDE "globals.asm"
INCLUDE "memcpy.asm" INCLUDE "memory.asm"
INCLUDE "hardwarectl.asm"
INCLUDE "interrupts.asm" INCLUDE "interrupts.asm"
INCLUDE "sprites.asm" INCLUDE "sprites.asm"
INCLUDE "rng.asm"
INCLUDE "res/tiles.inc" INCLUDE "res/tiles.inc"
INCLUDE "res/gameplay_map.inc" INCLUDE "res/gameplay_map.inc"
SECTION "Code Entry Point", ROM0 SECTION "Code Entry Point", ROM0
MainEntryPoint:: Main::
; Turn off LCD during initialization. ; Turn off LCD during initialization.
call DisableLCD wait_vram
xor a, a
ldh [rLCDC], a
; Save some power and turn off the audio. ; Save some power and turn off the audio.
call DisableAudio xor a, a
ldh [rNR52], a
; We use a single set of tiles for the entire game, so we copy it at the start. ; We use a single set of tiles for the entire game, so we copy it at the start.
ld de, Tiles ld de, Tiles
@ -27,7 +30,9 @@ MainEntryPoint::
call UnsafeMemCopy call UnsafeMemCopy
; Make sure both sprites and bg use the same tile data. ; Make sure both sprites and bg use the same tile data.
call SetTileDataBanks ldh a, [rLCDC]
or LCDCF_BLK01
ldh [rLCDC], a
; The tilemap is just for testing for now. ; The tilemap is just for testing for now.
ld de, GameplayTilemap ld de, GameplayTilemap
@ -44,6 +49,11 @@ MainEntryPoint::
ld a, PALETTE_REGULAR ld a, PALETTE_REGULAR
set_all_palettes set_all_palettes
; Get the timer going. It's used for RNG.
xor a, a
ldh [rTMA], a
ld a, TACF_262KHZ | TACF_START
; Zero out the ram where needed. ; Zero out the ram where needed.
call InitializeVariables call InitializeVariables
@ -51,12 +61,17 @@ MainEntryPoint::
call InitializeLCDCInterrupt call InitializeLCDCInterrupt
; And turn the LCD back on before we start. ; And turn the LCD back on before we start.
call EnableLCD ldh a, [rLCDC]
or LCDCF_ON | LCDCF_BGON | LCDCF_OBJON
ldh [rLCDC], a
; Make sure the first game loop starts just like all the future ones. ; Make sure the first game loop starts just like all the future ones.
wait_vblank wait_vblank
wait_vblank_end wait_vblank_end
; TEMP: Set up the game.
call StartNewGame
GameLoop:: GameLoop::
call GetInput call GetInput
@ -65,10 +80,18 @@ GameLoop::
; Handle gameplay here ; Handle gameplay here
; TODO ; TODO
ld a, 0 ldh a, [hCtr]
inc a
and a, $1F
ldh [hCtr], a
jr nz, :+
call GetNextPiece
: ld a, [wNextPiece]
call ApplyNext call ApplyNext
ld a, 4 ld a, [wNextPiece]
call ApplyHold call ApplyHold
ld hl, wSPRScore1 ld hl, wSPRScore1

View File

@ -1,5 +1,5 @@
#Emulicious settings file #Emulicious settings file
#Wed Oct 11 08:17:53 CEST 2023 #Fri Oct 13 08:36:54 CEST 2023
SouthPanelHeight=635 SouthPanelHeight=635
GameBoyErrorBreakpointMessage6= GameBoyErrorBreakpointMessage6=
GameBoyErrorBreakpointMessage5= GameBoyErrorBreakpointMessage5=
@ -46,7 +46,7 @@ GameBoyErrorBreakpointEnabled3=false
Gamepad0Key5=-1 Gamepad0Key5=-1
GameBoyErrorBreakpointEnabled2=false GameBoyErrorBreakpointEnabled2=false
Gamepad0Key4=-1 Gamepad0Key4=-1
WindowVideoViewerOpen=true WindowVideoViewerOpen=false
GameBoyErrorBreakpointEnabled1=false GameBoyErrorBreakpointEnabled1=false
Gamepad0Key3=-1 Gamepad0Key3=-1
GameBoyErrorBreakpointEnabled0=false GameBoyErrorBreakpointEnabled0=false
@ -63,7 +63,7 @@ WindowEventViewerWindowY=489
WindowEventViewerWindowX=1472 WindowEventViewerWindowX=1472
WindowSpriteViewerHeight=527 WindowSpriteViewerHeight=527
WindowSpriteViewerWidth=370 WindowSpriteViewerWidth=370
DebuggerWestPanelSelectedTab=1 DebuggerWestPanelSelectedTab=0
WindowMemoryTracerWindowHeight=289 WindowMemoryTracerWindowHeight=289
Gamepad0Key37=-1 Gamepad0Key37=-1
Gamepad0Key36=-1 Gamepad0Key36=-1
@ -76,7 +76,7 @@ Gamepad0Key32=-1
Gamepad0Key31=-1 Gamepad0Key31=-1
Gamepad0Key30=-1 Gamepad0Key30=-1
DebuggerSouthPanelSelectedTab=0 DebuggerSouthPanelSelectedTab=0
StackWidth=940 StackWidth=1311
SMSGamepadAKeyboard=false SMSGamepadAKeyboard=false
GameBoyErrorBreakpointCondition19= GameBoyErrorBreakpointCondition19=
GameBoyErrorBreakpointCondition18= GameBoyErrorBreakpointCondition18=
@ -148,33 +148,33 @@ Gamepad1Key21=-1
Gamepad1Key20=-1 Gamepad1Key20=-1
StackSplitLocation=320 StackSplitLocation=320
FontSize=13 FontSize=13
CombineVideoViewers=true
WindowEmuliciousHeight=781
CodeFontSize=13 CodeFontSize=13
WindowEmuliciousHeight=781
CombineVideoViewers=true
WindowTilemapViewerY=544 WindowTilemapViewerY=544
MemoryTracer=true
GBGamepadKeyboard=false
UninitializedMemoryBreakpointSuspend=true
WindowTilemapViewerX=1013 WindowTilemapViewerX=1013
UninitializedMemoryBreakpointSuspend=true
GBGamepadKeyboard=false
MemoryTracer=true
SMSGamepadBKeyboard=false SMSGamepadBKeyboard=false
DebuggerConsoleLogBreakpoints=true DebuggerConsoleLogBreakpoints=true
SMSGamepadBThreshold=50 SMSGamepadBThreshold=50
Gamepad1Key19=-1 Gamepad1Key19=-1
Gamepad1Key18=-1
BankSwapAtPCBreakpointEnabled=false BankSwapAtPCBreakpointEnabled=false
Gamepad1Key18=-1
Gamepad1Key17=-1 Gamepad1Key17=-1
WindowPaletteViewerWidth=485
Gamepad1Key16=-1 Gamepad1Key16=-1
Gamepad1Key15=-1 WindowPaletteViewerWidth=485
Recent0=C\:\\workspace\\dmgtris\\bin\\out.gb
GameBoyErrorBreakpointSuspend32=true GameBoyErrorBreakpointSuspend32=true
Recent0=C\:\\workspace\\dmgtris\\bin\\out.gb
Gamepad1Key15=-1
Gamepad1Key14=-1 Gamepad1Key14=-1
Gamepad1Key13=-1 Gamepad1Key13=-1
Gamepad1Key12=-1 Gamepad1Key12=-1
Gamepad1Key11=-1
GameBoyErrorBreakpointSuspend9=true GameBoyErrorBreakpointSuspend9=true
Gamepad1Key10=-1 Gamepad1Key11=-1
GameBoyErrorBreakpointSuspend8=true GameBoyErrorBreakpointSuspend8=true
Gamepad1Key10=-1
GameBoyErrorBreakpointSuspend7=true GameBoyErrorBreakpointSuspend7=true
GameBoyErrorBreakpointSuspend6=true GameBoyErrorBreakpointSuspend6=true
GameBoyErrorBreakpointSuspend5=true GameBoyErrorBreakpointSuspend5=true
@ -184,73 +184,73 @@ GameBoyErrorBreakpointSuspend2=true
GameBoyErrorBreakpointSuspend1=true GameBoyErrorBreakpointSuspend1=true
GameBoyErrorBreakpointSuspend0=true GameBoyErrorBreakpointSuspend0=true
Scale=5.0 Scale=5.0
DebuggerMemorySelectedAddress=0 DebuggerMemorySelectedAddress=390
WindowEmuliciousWidth=816 WindowEmuliciousWidth=816
GameBoyErrorBreakpointSuspend20=true GameBoyErrorBreakpointSuspend20=true
DataExecutionBreakpointCondition= DataExecutionBreakpointCondition=
GameBoyErrorBreakpointMessage32=
BankSwapAtPCBreakpointCondition=
WindowTilemapViewerWidth=404 WindowTilemapViewerWidth=404
BankSwapAtPCBreakpointCondition=
GameBoyErrorBreakpointMessage32=
SMSbuttonsThreshold=50 SMSbuttonsThreshold=50
WindowTileViewerY=549 WindowTileViewerY=549
Gamepad1Key9=-1
WindowTileViewerX=271 WindowTileViewerX=271
Gamepad1Key8=-1 Gamepad1Key9=-1
UninitializedMemoryBreakpointCondition= UninitializedMemoryBreakpointCondition=
Gamepad1Key7=-1 Gamepad1Key8=-1
GameBoyErrorBreakpointSuspend19=true GameBoyErrorBreakpointSuspend19=true
Gamepad1Key6=-1 Gamepad1Key7=-1
GameBoyErrorBreakpointSuspend18=true GameBoyErrorBreakpointSuspend18=true
Gamepad1Key5=-1 Gamepad1Key6=-1
GameBoyErrorBreakpointSuspend17=true GameBoyErrorBreakpointSuspend17=true
Gamepad1Key4=-1 Gamepad1Key5=-1
GameBoyErrorBreakpointSuspend16=true GameBoyErrorBreakpointSuspend16=true
WindowVideoViewerY=275 Gamepad1Key4=-1
Gamepad1Key3=-1 Gamepad1Key3=-1
WindowVideoViewerX=946 WindowVideoViewerY=275
Gamepad1Key2=-1 Gamepad1Key2=-1
WindowVideoViewerX=946
Gamepad1Key1=-1 Gamepad1Key1=-1
Gamepad1Key0=-1 Gamepad1Key0=-1
GameBoyErrorBreakpointSuspend10=true GameBoyErrorBreakpointSuspend10=true
WindowTilemapViewerHeight=744
WindowEventViewerWindowDivider=309 WindowEventViewerWindowDivider=309
WindowTilemapViewerHeight=744
GameBoyErrorBreakpointMessage20= GameBoyErrorBreakpointMessage20=
MemoryTracerDisplayFileOffsetsForROM=true
WindowTileViewerWidth=467 WindowTileViewerWidth=467
WindowDebuggerY=158 MemoryTracerDisplayFileOffsetsForROM=true
WindowDebuggerX=939 WindowDebuggerY=189
WindowDebuggerX=1113
Key37=-1 Key37=-1
Key36=-1 Key36=-1
Key35=-1
GameBoyErrorBreakpointCondition9=
GameBoyErrorBreakpointEnabled32=false
WindowTileViewerOpen=false WindowTileViewerOpen=false
Key34=-1 GameBoyErrorBreakpointEnabled32=false
GameBoyErrorBreakpointCondition9=
Key35=-1
GameBoyErrorBreakpointCondition8= GameBoyErrorBreakpointCondition8=
Key33=-1 Key34=-1
GameBoyErrorBreakpointMessage19=
GameBoyErrorBreakpointCondition7= GameBoyErrorBreakpointCondition7=
Key32=-1 GameBoyErrorBreakpointMessage19=
GameBoyErrorBreakpointCondition6= Key33=-1
GameBoyErrorBreakpointMessage18=
DebuggerMemoryTabVisibleRect=0,0,0,0 DebuggerMemoryTabVisibleRect=0,0,0,0
Key31=-1 GameBoyErrorBreakpointMessage18=
GameBoyErrorBreakpointCondition5= GameBoyErrorBreakpointCondition6=
GameBoyErrorBreakpointMessage17= Key32=-1
OutlineWidth=425
WindowEventViewerWindowWidth=930 WindowEventViewerWindowWidth=930
Key30=-1 OutlineWidth=425
GameBoyErrorBreakpointCondition4= GameBoyErrorBreakpointMessage17=
GameBoyErrorBreakpointCondition5=
Key31=-1
GameBoyErrorBreakpointMessage16= GameBoyErrorBreakpointMessage16=
GameBoyErrorBreakpointCondition4=
Key30=-1
GameBoyErrorBreakpointCondition3= GameBoyErrorBreakpointCondition3=
GameBoyErrorBreakpointCondition2= GameBoyErrorBreakpointCondition2=
GameBoyErrorBreakpointCondition1=
Key9=40 Key9=40
GameBoyErrorBreakpointCondition0= GameBoyErrorBreakpointCondition1=
Key8=38 Key8=38
GameBoyErrorBreakpointCondition0=
Key7=83 Key7=83
Key6=87
GameBoyErrorBreakpointMessage10= GameBoyErrorBreakpointMessage10=
Key6=87
Key5=65 Key5=65
Key4=68 Key4=68
Key3=10 Key3=10
@ -262,21 +262,21 @@ Update=2
Key29=-1 Key29=-1
Key28=10 Key28=10
Key27=-1 Key27=-1
WindowSpriteViewerOpen=false
Key26=-1 Key26=-1
WindowSpriteViewerOpen=false
Key25=-1 Key25=-1
Key24=-1
WindowTileViewerHeight=453 WindowTileViewerHeight=453
Key23=10 Key24=-1
GameBoyErrorBreakpointEnabled20=false GameBoyErrorBreakpointEnabled20=false
Key23=10
Key22=-1 Key22=-1
Key21=-1 Key21=-1
Key20=-1
BankSwapAtPCBreakpointSuspend=true BankSwapAtPCBreakpointSuspend=true
WindowEmuliciousY=376 Key20=-1
WindowEmuliciousX=146 WindowEmuliciousY=489
DebuggerMemorySelectedTab=HRAM WindowEmuliciousX=666
WindowDebuggerWidth=1110 DebuggerMemorySelectedTab=RAM
WindowDebuggerWidth=1481
GameBoyErrorBreakpointMessage9= GameBoyErrorBreakpointMessage9=
GameBoyErrorBreakpointMessage8= GameBoyErrorBreakpointMessage8=
GameBoyErrorBreakpointMessage7= GameBoyErrorBreakpointMessage7=