pokecrystal-board/engine/printer/printer_serial.asm

638 lines
12 KiB
NASM
Raw Normal View History

2018-06-24 07:09:41 -07:00
Printer_StartTransmission:
2016-05-11 14:16:03 -07:00
ld hl, wGameboyPrinterRAM
ld bc, wGameboyPrinterRAMEnd - wGameboyPrinterRAM
xor a
call Printer_ByteFill
xor a
ld [rSB], a
ld [rSC], a
ld [wPrinterOpcode], a
ld hl, wPrinterConnectionOpen
set 0, [hl]
2018-01-23 14:39:09 -08:00
ld a, [wGBPrinter]
2016-05-11 14:16:03 -07:00
ld [wGBPrinterSettings], a
xor a
ld [wJumptableIndex], a
ret
2018-06-24 07:09:41 -07:00
PrinterJumptableIteration:
2016-05-11 14:16:03 -07:00
ld a, [wJumptableIndex]
ld e, a
ld d, 0
ld hl, .Jumptable
add hl, de
add hl, de
ld a, [hli]
ld h, [hl]
ld l, a
jp hl
2016-05-11 14:16:03 -07:00
2018-06-24 18:39:23 -07:00
.Jumptable:
2016-05-11 14:16:03 -07:00
dw Print_InitPrinterHandshake ; 00
dw Printer_CheckConnectionStatus ; 01
dw Printer_WaitSerial ; 02
dw Printer_StartTransmittingTilemap ; 03
dw Printer_TransmissionLoop ; 04
dw Printer_WaitSerialAndLoopBack2 ; 05
dw Printer_EndTilemapTransmission ; 06
dw Printer_TransmissionLoop ; 07
dw Printer_WaitSerial ; 08
dw Printer_SignalSendHeader ; 09
dw Printer_TransmissionLoop ; 0a
dw Printer_WaitSerial ; 0b
dw Printer_WaitUntilFinished ; 0c
dw Printer_Quit ; 0d
dw Printer_NextSection ; 0e
2016-05-11 14:16:03 -07:00
dw Printer_WaitSerial ; 0f
dw Printer_SignalLoopBack ; 10
dw Printer_SectionOne ; 11
dw Printer_NextSectionWaitLoopBack ; 12
dw Printer_WaitLoopBack ; 13
2016-05-11 14:16:03 -07:00
_Printer_NextSection:
2016-05-11 14:16:03 -07:00
ld hl, wJumptableIndex
inc [hl]
ret
2018-06-24 07:09:41 -07:00
Printer_PrevSection:
2016-05-11 14:16:03 -07:00
ld hl, wJumptableIndex
dec [hl]
ret
2018-06-24 07:09:41 -07:00
Printer_Quit:
2016-05-11 14:16:03 -07:00
xor a
ld [wPrinterStatusFlags], a
ld hl, wJumptableIndex
set 7, [hl]
ret
Printer_NextSection:
call _Printer_NextSection
2016-05-11 14:16:03 -07:00
ret
2018-06-24 07:09:41 -07:00
Printer_SectionOne:
2016-05-11 14:16:03 -07:00
ld a, $1
ld [wJumptableIndex], a
ret
2018-06-24 07:09:41 -07:00
Print_InitPrinterHandshake:
2016-05-11 14:16:03 -07:00
call Printer_ResetData
ld hl, PrinterDataPacket1
call Printer_CopyPacket
xor a
ld [wPrinterSendByteCounter], a
ld [wPrinterSendByteCounter + 1], a
ld a, [wPrinterQueueLength]
ld [wPrinterRowIndex], a
call _Printer_NextSection
2016-05-11 14:16:03 -07:00
call Printer_WaitHandshake
ld a, PRINTER_STATUS_CHECKING
ld [wPrinterStatus], a
ret
2018-06-24 07:09:41 -07:00
Printer_StartTransmittingTilemap:
2016-05-11 14:16:03 -07:00
call Printer_ResetData
; check ???
ld hl, wPrinterRowIndex
ld a, [hl]
and a
jr z, Printer_EndTilemapTransmission
; send packet 3
ld hl, PrinterDataPacket3 ; signal start of transmission
call Printer_CopyPacket
; prepare to send 40 tiles
call Printer_Convert2RowsTo2bpp
ld a, LOW(40 tiles)
2016-05-11 14:16:03 -07:00
ld [wPrinterSendByteCounter], a
ld a, HIGH(40 tiles)
2016-05-11 14:16:03 -07:00
ld [wPrinterSendByteCounter + 1], a
; compute the checksum
call Printer_ComputeChecksum
call _Printer_NextSection
2016-05-11 14:16:03 -07:00
call Printer_WaitHandshake
ld a, PRINTER_STATUS_TRANSMITTING
ld [wPrinterStatus], a
ret
2018-06-24 07:09:41 -07:00
Printer_EndTilemapTransmission:
2016-05-11 14:16:03 -07:00
; ensure that we go from here to routine 7
ld a, $6
ld [wJumptableIndex], a
; send packet 4
ld hl, PrinterDataPacket4 ; signal no transmission
call Printer_CopyPacket
; send no tile data
xor a
ld [wPrinterSendByteCounter], a
ld [wPrinterSendByteCounter + 1], a
call _Printer_NextSection
2016-05-11 14:16:03 -07:00
call Printer_WaitHandshake
ret
2018-06-24 07:09:41 -07:00
Printer_SignalSendHeader:
2016-05-11 14:16:03 -07:00
call Printer_ResetData
ld hl, PrinterDataPacket2 ; signal request print
call Printer_CopyPacket
; prepare to send 1 tile
call Printer_StageHeaderForSend
ld a, LOW(4)
2016-05-11 14:16:03 -07:00
ld [wPrinterSendByteCounter], a
ld a, HIGH(4)
2016-05-11 14:16:03 -07:00
ld [wPrinterSendByteCounter + 1], a
; compute the checksum
call Printer_ComputeChecksum
call _Printer_NextSection
2016-05-11 14:16:03 -07:00
call Printer_WaitHandshake
ld a, PRINTER_STATUS_PRINTING
ld [wPrinterStatus], a
ret
2018-06-24 07:09:41 -07:00
Printer_SignalLoopBack:
2016-05-11 14:16:03 -07:00
call Printer_ResetData
; send packet 1
ld hl, PrinterDataPacket1 ; signal no transmission
call Printer_CopyPacket
; send no tile data
xor a
ld [wPrinterSendByteCounter], a
ld [wPrinterSendByteCounter + 1], a
ld a, [wPrinterQueueLength]
ld [wPrinterRowIndex], a
call _Printer_NextSection
2016-05-11 14:16:03 -07:00
call Printer_WaitHandshake
ret
2018-06-24 07:09:41 -07:00
Printer_WaitSerial:
2016-05-11 14:16:03 -07:00
ld hl, wPrinterSerialFrameDelay
inc [hl]
ld a, [hl]
cp $6
ret c
xor a
ld [hl], a
call _Printer_NextSection
2016-05-11 14:16:03 -07:00
ret
2018-06-24 07:09:41 -07:00
Printer_WaitSerialAndLoopBack2:
2016-05-11 14:16:03 -07:00
ld hl, wPrinterSerialFrameDelay
inc [hl]
ld a, [hl]
cp $6
ret c
xor a
ld [hl], a
ld hl, wPrinterRowIndex
dec [hl]
call Printer_PrevSection
call Printer_PrevSection
ret
2018-06-24 07:09:41 -07:00
Printer_CheckConnectionStatus:
2016-05-11 14:16:03 -07:00
ld a, [wPrinterOpcode]
and a
ret nz
ld a, [wPrinterHandshake]
cp $ff
jr nz, .printer_connected
ld a, [wPrinterStatusFlags]
cp $ff
jr z, .printer_error
.printer_connected
ld a, [wPrinterHandshake]
cp $81
jr nz, .printer_error
ld a, [wPrinterStatusFlags]
cp $0
jr nz, .printer_error
ld hl, wPrinterConnectionOpen
set 1, [hl]
ld a, $5
ld [wHandshakeFrameDelay], a
call _Printer_NextSection
2016-05-11 14:16:03 -07:00
ret
.printer_error
ld a, $ff
ld [wPrinterHandshake], a
ld [wPrinterStatusFlags], a
ld a, $e
ld [wJumptableIndex], a
ret
2018-06-24 07:09:41 -07:00
Printer_TransmissionLoop:
2016-05-11 14:16:03 -07:00
ld a, [wPrinterOpcode]
and a
ret nz
ld a, [wPrinterStatusFlags]
and $f0
jr nz, .enter_wait_loop
ld a, [wPrinterStatusFlags]
and $1
jr nz, .cycle_back
call _Printer_NextSection
2016-05-11 14:16:03 -07:00
ret
.cycle_back
call Printer_PrevSection
ret
.enter_wait_loop
ld a, $12 ; Printer_NextSectionWaitLoopBack
2016-05-11 14:16:03 -07:00
ld [wJumptableIndex], a
ret
2018-06-24 07:09:41 -07:00
Printer_WaitUntilFinished:
2016-05-11 14:16:03 -07:00
ld a, [wPrinterOpcode]
and a
ret nz
ld a, [wPrinterStatusFlags]
and $f3
ret nz
call _Printer_NextSection
2016-05-11 14:16:03 -07:00
ret
Printer_NextSectionWaitLoopBack:
call _Printer_NextSection
2018-06-24 07:09:41 -07:00
Printer_WaitLoopBack:
2016-05-11 14:16:03 -07:00
ld a, [wPrinterOpcode]
and a
ret nz
ld a, [wPrinterStatusFlags]
and $f0
ret nz
xor a
ld [wJumptableIndex], a
ret
2018-06-24 07:09:41 -07:00
Printer_WaitHandshake:
2016-05-11 14:16:03 -07:00
.loop
ld a, [wPrinterOpcode]
and a
jr nz, .loop
xor a
ld [wPrinterSendByteOffset], a
ld [wPrinterSendByteOffset + 1], a
ld a, $1
ld [wPrinterOpcode], a
ld a, $88
ld [rSB], a
ld a, $1
ld [rSC], a
ld a, $81
ld [rSC], a
ret
2018-06-24 07:09:41 -07:00
Printer_CopyPacket:
2016-05-11 14:16:03 -07:00
ld a, [hli]
2017-12-09 12:28:03 -08:00
ld [wPrinterData], a
2016-05-11 14:16:03 -07:00
ld a, [hli]
2017-12-09 12:28:03 -08:00
ld [wPrinterData + 1], a
2016-05-11 14:16:03 -07:00
ld a, [hli]
2017-12-09 12:28:03 -08:00
ld [wPrinterData + 2], a
2016-05-11 14:16:03 -07:00
ld a, [hli]
2017-12-09 12:28:03 -08:00
ld [wPrinterData + 3], a
2016-05-11 14:16:03 -07:00
ld a, [hli]
ld [wPrinterChecksum], a
ld a, [hl]
ld [wPrinterChecksum + 1], a
ret
2018-06-24 07:09:41 -07:00
Printer_ResetData:
2016-05-11 14:16:03 -07:00
xor a
2017-12-09 12:28:03 -08:00
ld hl, wPrinterData
2016-05-11 14:16:03 -07:00
ld [hli], a
ld [hli], a
ld [hli], a
ld [hl], a
ld hl, wPrinterChecksum
ld [hli], a
ld [hl], a
xor a
ld [wPrinterSendByteCounter], a
ld [wPrinterSendByteCounter + 1], a
ld hl, wGameboyPrinterRAM
ld bc, wGameboyPrinter2bppSourceEnd - wGameboyPrinter2bppSource
call Printer_ByteFill
ret
2018-06-24 07:09:41 -07:00
Printer_ComputeChecksum:
2016-05-11 14:16:03 -07:00
ld hl, 0
ld bc, 4
2017-12-09 12:28:03 -08:00
ld de, wPrinterData
2016-05-11 14:16:03 -07:00
call .ComputeChecksum
ld a, [wPrinterSendByteCounter]
ld c, a
ld a, [wPrinterSendByteCounter + 1]
ld b, a
ld de, wGameboyPrinterRAM
call .ComputeChecksum
ld a, l
ld [wPrinterChecksum], a
ld a, h
ld [wPrinterChecksum + 1], a
ret
2018-06-24 07:09:41 -07:00
.ComputeChecksum:
2016-05-11 14:16:03 -07:00
.loop
ld a, [de]
inc de
add l
jr nc, .no_overflow
inc h
.no_overflow
ld l, a
dec bc
ld a, c
or b
jr nz, .loop
ret
2018-06-24 07:09:41 -07:00
Printer_StageHeaderForSend:
2016-05-11 14:16:03 -07:00
ld a, $1
ld [wGameboyPrinter2bppSource + 0], a
ld a, [wcbfa]
ld [wGameboyPrinter2bppSource + 1], a
ld a, %11100100
ld [wGameboyPrinter2bppSource + 2], a
ld a, [wGBPrinterSettings]
ld [wGameboyPrinter2bppSource + 3], a
ret
2018-06-24 07:09:41 -07:00
Printer_Convert2RowsTo2bpp:
2016-05-11 14:16:03 -07:00
; de = wPrinterTileMapBuffer + 2 * SCREEN_WIDTH * ([wPrinterQueueLength] - [wPrinterRowIndex])
ld a, [wPrinterRowIndex]
xor $ff
ld d, a
ld a, [wPrinterQueueLength]
inc a
add d
ld hl, wPrinterTileMapBuffer
ld de, 2 * SCREEN_WIDTH
.loop1
and a
jr z, .okay1
add hl, de
dec a
jr .loop1
.okay1
ld e, l
ld d, h
ld hl, wGameboyPrinter2bppSource
ld c, 2 * SCREEN_WIDTH
.loop2
ld a, [de]
inc de
push bc
push de
push hl
; convert tile index to vram address
swap a
ld d, a
and $f0
ld e, a
ld a, d
and $f
ld d, a
and $8
ld a, d
jr nz, .vtiles_8xxx
or $90
jr .got_vtile_addr
.vtiles_8xxx
or $80
.got_vtile_addr
ld d, a
; copy 1 vtile to hl
lb bc, BANK(Printer_Convert2RowsTo2bpp), 1
call Request2bpp
pop hl
ld de, 1 tiles
add hl, de
pop de
pop bc
dec c
jr nz, .loop2
ret
2018-06-24 07:09:41 -07:00
Printer_ByteFill:
2016-05-11 14:16:03 -07:00
push de
ld e, a
.loop
ld [hl], e
inc hl
dec bc
ld a, c
or b
jr nz, .loop
ld a, e
pop de
ret
PrinterDataPacket1:
db 1, 0, $00, 0
dw 1
PrinterDataPacket2:
db 2, 0, $04, 0
dw 0
PrinterDataPacket3:
db 4, 0, $80, 2
dw 0
PrinterDataPacket4:
db 4, 0, $00, 0
dw 4
PrinterDataPacket5: ; unused
db 8, 0, $00, 0
dw 8
PrinterDataPacket6: ; unused
db 15, 0, $00, 0
dw 15
2018-06-24 07:09:41 -07:00
_PrinterReceive::
2016-05-11 14:16:03 -07:00
ld a, [wPrinterOpcode]
add a
ld e, a
ld d, 0
ld hl, .Jumptable
add hl, de
ld a, [hli]
ld h, [hl]
ld l, a
jp hl
2016-05-11 14:16:03 -07:00
2018-06-24 07:09:41 -07:00
.Jumptable:
2016-05-11 14:16:03 -07:00
dw Printer_DoNothing ; 00
dw Printer_Send0x33 ; 01
2017-12-09 12:28:03 -08:00
dw Printer_SendPrinterData1 ; 02
dw Printer_SendPrinterData2 ; 03
dw Printer_SendPrinterData3 ; 04
dw Printer_SendPrinterData4 ; 05
2016-05-11 14:16:03 -07:00
dw Printer_SendNextByte ; 06
dw Printer_SendwPrinterChecksumLo ; 07
dw Printer_SendwPrinterChecksumHi ; 08
dw Printer_Send0x00_2 ; 09
dw Printer_ReceiveTwoPrinterHandshakeAndSend0x00 ; 0a
dw Printer_ReceiveTwoPrinterStatusFlagsAndExitSendLoop ; 0b
2016-05-11 14:16:03 -07:00
dw Printer_Send0x33 ; 0c triggered by AskSerial
dw Printer_Send0x0f ; 0d
dw Printer_Send0x00 ; 0e
dw Printer_Send0x00 ; 0f
dw Printer_Send0x00 ; 10
2016-05-11 14:16:03 -07:00
dw Printer_Send0x0f ; 11
dw Printer_Send0x00 ; 12
dw Printer_Send0x00_2 ; 13
dw Printer_ReceiveTwoPrinterHandshakeAndSend0x00 ; 14
dw Printer_ReceiveTwoPrinterStatusFlagsAndExitSendLoop_2 ; 15
2016-05-11 14:16:03 -07:00
dw Printer_Send0x33 ; 16 triggered by pressing B
dw Printer_Send0x08 ; 17
dw Printer_Send0x00 ; 18
dw Printer_Send0x00 ; 19
dw Printer_Send0x00 ; 1a
2016-05-11 14:16:03 -07:00
dw Printer_Send0x08 ; 1b
dw Printer_Send0x00 ; 1c
dw Printer_Send0x00_2 ; 1d
dw Printer_ReceiveTwoPrinterHandshakeAndSend0x00 ; 1e
dw Printer_ReceiveTwoPrinterStatusFlagsAndExitSendLoop ; 1f
2016-05-11 14:16:03 -07:00
2018-06-24 07:09:41 -07:00
Printer_NextInstruction:
2016-05-11 14:16:03 -07:00
ld hl, wPrinterOpcode
inc [hl]
ret
2018-06-24 07:09:41 -07:00
Printer_DoNothing:
2016-05-11 14:16:03 -07:00
ret
2018-06-24 07:09:41 -07:00
Printer_Send0x33:
2016-05-11 14:16:03 -07:00
ld a, $33
call Printer_SerialSend
call Printer_NextInstruction
ret
2018-06-24 07:09:41 -07:00
Printer_SendPrinterData1:
2017-12-09 12:28:03 -08:00
ld a, [wPrinterData]
2016-05-11 14:16:03 -07:00
call Printer_SerialSend
call Printer_NextInstruction
ret
2018-06-24 07:09:41 -07:00
Printer_SendPrinterData2:
2017-12-09 12:28:03 -08:00
ld a, [wPrinterData + 1]
2016-05-11 14:16:03 -07:00
call Printer_SerialSend
call Printer_NextInstruction
ret
2018-06-24 07:09:41 -07:00
Printer_SendPrinterData3:
2017-12-09 12:28:03 -08:00
ld a, [wPrinterData + 2]
2016-05-11 14:16:03 -07:00
call Printer_SerialSend
call Printer_NextInstruction
ret
2018-06-24 07:09:41 -07:00
Printer_SendPrinterData4:
2017-12-09 12:28:03 -08:00
ld a, [wPrinterData + 3]
2016-05-11 14:16:03 -07:00
call Printer_SerialSend
call Printer_NextInstruction
ret
2018-06-24 07:09:41 -07:00
Printer_SendNextByte:
2016-05-11 14:16:03 -07:00
; decrement 16-bit counter
ld hl, wPrinterSendByteCounter
ld a, [hli]
ld d, [hl]
ld e, a
or d
jr z, .done
dec de
ld [hl], d
dec hl
ld [hl], e
ld a, [wPrinterSendByteOffset]
ld e, a
ld a, [wPrinterSendByteOffset + 1]
ld d, a
ld hl, wGameboyPrinterRAM
add hl, de
inc de
ld a, e
ld [wPrinterSendByteOffset], a
ld a, d
ld [wPrinterSendByteOffset + 1], a
ld a, [hl]
call Printer_SerialSend
ret
.done
call Printer_NextInstruction
2018-06-24 07:09:41 -07:00
Printer_SendwPrinterChecksumLo:
2016-05-11 14:16:03 -07:00
ld a, [wPrinterChecksum]
call Printer_SerialSend
call Printer_NextInstruction
ret
2018-06-24 07:09:41 -07:00
Printer_SendwPrinterChecksumHi:
2016-05-11 14:16:03 -07:00
ld a, [wPrinterChecksum + 1]
call Printer_SerialSend
call Printer_NextInstruction
ret
Printer_Send0x00_2:
; identical to Printer_Send0x00, but referenced less
2016-05-11 14:16:03 -07:00
ld a, $0
call Printer_SerialSend
call Printer_NextInstruction
ret
Printer_ReceiveTwoPrinterHandshakeAndSend0x00:
2016-05-11 14:16:03 -07:00
ld a, [rSB]
ld [wPrinterHandshake], a
ld a, $0
call Printer_SerialSend
call Printer_NextInstruction
ret
Printer_ReceiveTwoPrinterStatusFlagsAndExitSendLoop:
2016-05-11 14:16:03 -07:00
ld a, [rSB]
ld [wPrinterStatusFlags], a
xor a
ld [wPrinterOpcode], a
ret
2018-06-24 07:09:41 -07:00
Printer_Send0x0f:
2016-05-11 14:16:03 -07:00
ld a, $f
call Printer_SerialSend
call Printer_NextInstruction
ret
Printer_Send0x00:
2016-05-11 14:16:03 -07:00
ld a, $0
call Printer_SerialSend
call Printer_NextInstruction
ret
2018-06-24 07:09:41 -07:00
Printer_Send0x08:
2016-05-11 14:16:03 -07:00
ld a, $8
call Printer_SerialSend
call Printer_NextInstruction
ret
2018-06-24 07:09:41 -07:00
Printer_SerialSend:
2016-05-11 14:16:03 -07:00
ld [rSB], a
ld a, $1 ; switch to internal clock
ld [rSC], a
ld a, $81 ; start transfer
ld [rSC], a
ret
Printer_ReceiveTwoPrinterStatusFlagsAndExitSendLoop_2:
; identical to Printer_ReceiveTwoPrinterStatusFlagsAndExitSendLoop, but referenced less
2016-05-11 14:16:03 -07:00
ld a, [rSB]
ld [wPrinterStatusFlags], a
xor a
ld [wPrinterOpcode], a
ret