; Shared by every probe: register names, start-up, a text screen, battery RAM. ; ; A probe includes this first, defines Reset, Title (21 bytes) and its own ; strings, and ends with `ProbeTail Title`, which places the LoROM ; header (32 KiB, 2 KiB battery RAM, Japan) and the vectors. The build script ; generates font.inc and fixes the header checksum. ; ; Screen: mode 0, BG1 only, 32x28 text cells, white on dark blue. Text is ; written during force blank only; a probe that measures with the display on ; blanks again before printing its results. .p816 .smart - INIDISP = $2100 OAMADDL = $2102 OAMADDH = $2103 OAMDATA = $2104 BGMODE = $2105 BG1SC = $2107 BG12NBA = $210B BG1HOFS = $210D BG1VOFS = $210E VMAIN = $2115 VMADD = $2116 VMDATA = $2118 M7A = $211B M7B = $211C CGADD = $2121 CGDATA = $2122 TM = $212C TS = $212D CGWSEL = $2130 CGADSUB = $2131 COLDATA = $2132 SETINI = $2133 MPYL = $2134 SLHV = $2137 RDOAM = $2138 RDCGRAM = $213B OPHCT = $213C OPVCT = $213D STAT77 = $213E STAT78 = $213F APUIO0 = $2140 APUIO1 = $2141 APUIO2 = $2142 APUIO3 = $2143 NMITIMEN = $4200 WRIO = $4201 HTIMEL = $4207 HTIMEH = $4208 VTIMEL = $4209 VTIMEH = $420A RDNMI = $4210 TIMEUP = $4211 HVBJOY = $4212 SRAM = $700000 MAP = $0400 ; BG1 tilemap, VRAM word address RESULTS = $0200 ; WRAM buffer copied to battery RAM by SaveResults ; Direct page $00-$0F is the library's; probes use $10 and up. text_ptr = $00 ; 16-bit hex_tmp = $02 ; Print a zero-terminated string at row r, column 2. .macro PrintAt r, str ldx #MAP + (r)*32 + 2 ldy #str jsr Print .endmacro ; Native mode, 8-bit A, 16-bit X/Y, DBR = DP = 0, stack at $1FFF, force blank, ; no interrupts. Leaves the PPU untouched so a probe can read power-on state. .macro ProbeStart sei clc xce rep #$38 .a16 .i16 ldx #$1FFF txs lda #$0000 tcd sep #$20 .a8 pha plb lda #$80 sta INIDISP stz NMITIMEN .endmacro .segment "CODE" ; Every routine here runs with 8-bit A and 16-bit X/Y (ProbeStart sets it). .a8 .i16 ; Mode 0, BG1 only, font at VRAM word 0, map at MAP, palette 0 = blue/white. ; Clears all of VRAM. InitPPU: stz BGMODE lda #(MAP >> 10) << 2 sta BG1SC stz BG12NBA stz BG1HOFS stz BG1HOFS lda #$FF ; the first visible line shows BG line 0 sta BG1VOFS stz BG1VOFS lda #$01 sta TM stz TS stz CGWSEL stz CGADSUB lda #$E0 sta COLDATA stz SETINI lda #$80 sta VMAIN ; increment after the high byte rep #$20 .a16 stz VMADD ldx #$8000 : stz VMDATA dex bne :- stz VMADD ; font: 64 glyphs x 8 words, from ASCII 32 ldx #$0000 : lda Font,x sta VMDATA inx inx cpx #64*16 bne :- sep #$20 .a8 stz CGADD lda #$00 ; colour 0: dark blue (BGR555 $3000) sta CGDATA lda #$30 sta CGDATA lda #$FF ; colour 1: white sta CGDATA lda #$7F sta CGDATA rts ; Wait for the start of vertical blank ($4212 bit 7 rising). WaitVblankStart: : bit HVBJOY bmi :- : bit HVBJOY bpl :- rts ; Display on at full brightness, then stop. ShowAndHalt: lda #$0F sta INIDISP : bra :- ; X = VRAM word address, Y = address of a zero-terminated string in bank 0. ; Leaves VMADD after the last character so Put* calls can follow on. Print: stx VMADD PrintHere: sty text_ptr ldy #$0000 : lda (text_ptr),y beq :+ jsr PutChar iny bra :- : rts ; A = ASCII character, written at the current VRAM address. PutChar: sec sbc #32 sta VMDATA stz VMDATA+1 rts ; A = byte, printed as two hex digits. PrintHex: sta hex_tmp lsr a lsr a lsr a lsr a jsr PutNibble lda hex_tmp and #$0F PutNibble: phx rep #$20 ; TAX moves all 16 bits: clear the hidden B byte .a16 and #$000F tax sep #$20 .a8 lda HexDigits,x plx jmp PutChar ; Copy Y bytes from RESULTS to battery RAM at $70:0000. The probe has put its ; 8-byte magic at RESULTS and its payload after it. SaveResults: sty hex_tmp ldx #$0000 : lda RESULTS,x sta f:SRAM,x inx cpx hex_tmp bne :- rts EmptyHandler: rti .segment "RODATA" HexDigits: .byte "0123456789ABCDEF" .include "font.inc" ; outside ProbeTail: an .include in a macro lands after it ; Header and vectors; title must be exactly 21 characters. ram: the header's SRAM ; size byte (1 << n KiB), 1 (2 KiB) when left out. .macro ProbeTail title, ram .assert .strlen(title) = 21, error, "header title must be 21 characters" .segment "HEADER" .byte title .byte $20 ; LoROM, SlowROM .byte $02 ; ROM + RAM + battery .byte $05 ; ROM 32 KiB .ifblank ram .byte $01 ; SRAM 2 KiB .else .byte ram .endif .byte $00 ; region Japan (NTSC) .byte $00 ; developer .byte $00 ; version .word $FFFF ; checksum complement, fixed by the build .word $0000 ; checksum, fixed by the build .segment "VECTORS" ; native: -, -, COP, BRK, ABORT, NMI, -, IRQ .word 0, 0, EmptyHandler, EmptyHandler, EmptyHandler, EmptyHandler, 0, EmptyHandler ; emulation: -, -, COP, -, ABORT, NMI, RESET, IRQ/BRK .word 0, 0, EmptyHandler, 0, EmptyHandler, EmptyHandler, Reset, EmptyHandler .endmacro