1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
|
const_def
const PRINTER_STATUS_BLANK
const PRINTER_STATUS_CHECKING_LINK
const PRINTER_STATUS_TRANSMITTING
const PRINTER_STATUS_PRINTING
const PRINTER_ERROR_1
const PRINTER_ERROR_2
const PRINTER_ERROR_3
const PRINTER_ERROR_4
const PRINTER_ERROR_WRONG_DEVICE
StartTransmission_Send9Rows:
ld a, 9
Printer_StartTransmission:
push af
ld hl, wPrinterData
ld bc, wPrinterDataEnd - wPrinterData
xor a
call Printer_FillMemory
xor a
ldh [rSB], a
ldh [rSC], a
ld [wPrinterOpcode], a
ld hl, wPrinterConnectionOpen
set 0, [hl]
ld a, [wPrinterSettings]
ld [wPrinterSettingsTempCopy], a
pop af
ld [wPrinterQueueLength], a
ret
PrinterTransmissionJumptable:
ld a, [wPrinterSendState]
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
.Jumptable:
dw Printer_InitSerial ; 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_Next_ ; 0e
dw Printer_WaitSerial ; 0f
dw Printer_SignalLoopBack ; 10
dw Printer_LoopBack ; 11
dw Printer_WaitLoopBack ; 12
dw Printer_WaitLoopBack_ ; 13
Printer_Next:
ld hl, wPrinterSendState
inc [hl]
ret
Printer_Back:
ld hl, wPrinterSendState
dec [hl]
ret
Printer_Quit:
xor a
ld [wPrinterStatusFlags], a
ld hl, wPrinterSendState
set 7, [hl]
ret
Printer_Next_:
call Printer_Next
ret
Printer_LoopBack:
ld a, $1
ld [wPrinterSendState], a
ret
Printer_InitSerial:
call ResetPrinterData
ld hl, PrinterDataPacket1
call CopyPrinterDataHeader
xor a
ld [wPrinterDataSize], a
ld [wPrinterDataSize + 1], a
ld a, [wPrinterQueueLength]
ld [wPrinterRowIndex], a
call Printer_Next
call Printer_PrepareToSend
ld a, PRINTER_STATUS_CHECKING_LINK
ld [wPrinterStatusIndicator], a
ret
Printer_StartTransmittingTilemap:
call ResetPrinterData
ld hl, wPrinterRowIndex
ld a, [hl]
and a
jr z, Printer_EndTilemapTransmission
ld hl, PrinterDataPacket3
call CopyPrinterDataHeader
call Printer_Convert2RowsTo2bpp
ld a, LOW(wPrinterSendDataSource1End - wPrinterSendDataSource1)
ld [wPrinterDataSize], a
ld a, HIGH(wPrinterSendDataSource1End - wPrinterSendDataSource1)
ld [wPrinterDataSize + 1], a
call ComputePrinterChecksum
call Printer_Next
call Printer_PrepareToSend
ld a, PRINTER_STATUS_TRANSMITTING
ld [wPrinterStatusIndicator], a
ret
Printer_EndTilemapTransmission:
ld a, $6
ld [wPrinterSendState], a
ld hl, PrinterDataPacket4
call CopyPrinterDataHeader
xor a
ld [wPrinterDataSize], a
ld [wPrinterDataSize + 1], a
call Printer_Next
call Printer_PrepareToSend
ret
Printer_SignalSendHeader:
call ResetPrinterData
ld hl, PrinterDataPacket2
call CopyPrinterDataHeader
call Printer_StageHeaderForSend
ld a, $4
ld [wPrinterDataSize], a
ld a, $0
ld [wPrinterDataSize + 1], a
call ComputePrinterChecksum
call Printer_Next
call Printer_PrepareToSend
ld a, PRINTER_STATUS_PRINTING
ld [wPrinterStatusIndicator], a
ret
Printer_SignalLoopBack:
call ResetPrinterData
ld hl, PrinterDataPacket1
call CopyPrinterDataHeader
xor a
ld [wPrinterDataSize], a
ld [wPrinterDataSize + 1], a
ld a, [wPrinterQueueLength]
ld [wPrinterRowIndex], a
call Printer_Next
call Printer_PrepareToSend
ret
Printer_WaitSerial:
ld hl, wPrinterSerialFrameDelay
inc [hl]
ld a, [hl]
cp $6
ret c
xor a
ld [hl], a
call Printer_Next
ret
Printer_WaitSerialAndLoopBack2:
ld hl, wPrinterSerialFrameDelay
inc [hl]
ld a, [hl]
cp $6
ret c
xor a
ld [hl], a
ld hl, wPrinterRowIndex
dec [hl]
call Printer_Back
call Printer_Back
ret
Printer_CheckConnectionStatus:
ld a, [wPrinterOpcode]
and a
ret nz
ld a, [wPrinterHandshake]
cp $ff
jr nz, .asm_e88dc
ld a, [wPrinterStatusFlags]
cp $ff
jr z, .asm_e88f8
.asm_e88dc
ld a, [wPrinterHandshake]
cp $81
jr nz, .asm_e88f8
ld a, [wPrinterStatusFlags]
cp $0
jr nz, .asm_e88f8
ld hl, wPrinterConnectionOpen
set 1, [hl]
ld a, $5
ld [wHandshakeFrameDelay], a
call Printer_Next
ret
.asm_e88f8
ld a, $ff
ld [wPrinterHandshake], a
ld [wPrinterStatusFlags], a
ld a, $e
ld [wPrinterSendState], a
ret
Printer_TransmissionLoop:
ld a, [wPrinterOpcode]
and a
ret nz
ld a, [wPrinterStatusFlags]
and $f0
jr nz, .asm_e8921
ld a, [wPrinterStatusFlags]
and $1
jr nz, .asm_e891d
call Printer_Next
ret
.asm_e891d
call Printer_Back
ret
.asm_e8921
ld a, $12
ld [wPrinterSendState], a
ret
Printer_WaitUntilFinished:
ld a, [wPrinterOpcode]
and a
ret nz
ld a, [wPrinterStatusFlags]
and $f3
ret nz
call Printer_Next
ret
Printer_WaitLoopBack:
call Printer_Next
Printer_WaitLoopBack_:
ld a, [wPrinterOpcode]
and a
ret nz
ld a, [wPrinterStatusFlags]
and $f0
ret nz
xor a
ld [wPrinterSendState], a
ret
Printer_PrepareToSend:
.wait_printer_operation
ld a, [wPrinterOpcode]
and a
jr nz, .wait_printer_operation
xor a
ld [wPrinterSendByteOffset], a
ld [wPrinterSendByteOffset + 1], a
ld a, $1
ld [wPrinterOpcode], a
ld a, $88
ldh [rSB], a
ld a, $1
ldh [rSC], a
ld a, $81
ldh [rSC], a
ret
CopyPrinterDataHeader:
ld a, [hli]
ld [wPrinterDataHeader], a
ld a, [hli]
ld [wPrinterDataHeader + 1], a
ld a, [hli]
ld [wPrinterDataHeader + 2], a
ld a, [hli]
ld [wPrinterDataHeader + 3], a
ld a, [hli]
ld [wPrinterChecksum], a
ld a, [hl]
ld [wPrinterChecksum + 1], a
ret
ResetPrinterData:
xor a
ld hl, wPrinterDataHeader
ld [hli], a
ld [hli], a
ld [hli], a
ld [hl], a
ld hl, wPrinterChecksum
ld [hli], a
ld [hl], a
xor a
ld [wPrinterDataSize], a
ld [wPrinterDataSize + 1], a
ld hl, wPrinterSendDataSource1
ld bc, wPrinterSendDataSource1End - wPrinterSendDataSource1
call Printer_FillMemory
ret
ComputePrinterChecksum:
ld hl, $0
ld bc, $4
ld de, wPrinterDataHeader
call .AddToChecksum
ld a, [wPrinterDataSize]
ld c, a
ld a, [wPrinterDataSize + 1]
ld b, a
ld de, wPrinterSendDataSource1
call .AddToChecksum
ld a, l
ld [wPrinterChecksum], a
ld a, h
ld [wPrinterChecksum + 1], a
ret
.AddToChecksum:
.loop
ld a, [de]
inc de
add l
jr nc, .no_carry
inc h
.no_carry
ld l, a
dec bc
ld a, c
or b
jr nz, .loop
ret
Printer_StageHeaderForSend:
ld a, $1
ld [wPrinterSendDataSource1], a
ld a, [wcae2]
ld [wPrinterSendDataSource1 + 1], a
ld a, %11100100
ld [wPrinterSendDataSource1 + 2], a
ld a, [wPrinterSettingsTempCopy]
ld [wPrinterSendDataSource1 + 3], a
ret
Printer_Convert2RowsTo2bpp:
ld a, [wPrinterRowIndex]
ld b, a
ld a, [wPrinterQueueLength]
sub b
ld hl, wPrinterTileBuffer
ld de, 2 * SCREEN_WIDTH
.get_row
and a
jr z, .got_row
add hl, de
dec a
jr .get_row
.got_row
ld e, l
ld d, h
ld hl, wPrinterSendDataSource1
ld c, 2 * SCREEN_WIDTH
.loop
ld a, [de]
inc de
push bc
push de
push hl
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, .vchars1
or $90
jr .got_addr
.vchars1
or $80
.got_addr
ld d, a
lb bc, BANK(Printer_Convert2RowsTo2bpp), 1
call CopyVideoData
pop hl
ld de, $10
add hl, de
pop de
pop bc
dec c
jr nz, .loop
ret
Printer_FillMemory:
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
PrinterSerial_::
ld a, [wPrinterOpcode]
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
.Jumptable:
dw .Nop
dw .SignalTransmissionStart
dw .SendHeaderByte1
dw .SendHeaderByte2
dw .SendHeaderByte3
dw .SendHeaderByte4
dw .DataByte
dw .SendChecksumLo
dw .SendChecksumHi
dw .SignalTransmissionEnd
dw .Receive1
dw .Receive2
dw .SignalTransmissionStart
dw .Send_0F
dw .Send_00
dw .Send_00
dw .Send_00
dw .Send_0F
dw .Send_00
dw .SignalTransmissionEnd
dw .Receive1
dw .Receive2_
dw .SignalTransmissionStart
dw .SignalQuit
dw .Send_00
dw .Send_00
dw .Send_00
dw .SignalQuit
dw .Send_00
dw .SignalTransmissionEnd
dw .Receive1
dw .Receive2
.NextInstruction:
ld hl, wPrinterOpcode
inc [hl]
ret
.Nop:
ret
.SignalTransmissionStart:
ld a, $33
call .SendByte
call .NextInstruction
ret
.SendHeaderByte1:
ld a, [wPrinterDataHeader]
call .SendByte
call .NextInstruction
ret
.SendHeaderByte2:
ld a, [wPrinterDataHeader + 1]
call .SendByte
call .NextInstruction
ret
.SendHeaderByte3:
ld a, [wPrinterDataHeader + 2]
call .SendByte
call .NextInstruction
ret
.SendHeaderByte4:
ld a, [wPrinterDataHeader + 3]
call .SendByte
call .NextInstruction
ret
.DataByte:
ld hl, wPrinterDataSize
ld e, [hl]
inc hl
ld d, [hl]
ld a, e
or d
jr z, .sent_last_byte
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, wPrinterSendDataSource1
add hl, de
inc de
ld a, e
ld [wPrinterSendByteOffset], a
ld a, d
ld [wPrinterSendByteOffset + 1], a
ld a, [hl]
call .SendByte
ret
.sent_last_byte
call .NextInstruction
.SendChecksumLo:
ld a, [wPrinterChecksum]
call .SendByte
call .NextInstruction
ret
.SendChecksumHi:
ld a, [wPrinterChecksum + 1]
call .SendByte
call .NextInstruction
ret
.SignalTransmissionEnd:
ld a, $0
call .SendByte
call .NextInstruction
ret
.Receive1:
ldh a, [rSB]
ld [wPrinterHandshake], a
ld a, $0
call .SendByte
call .NextInstruction
ret
.Receive2:
ldh a, [rSB]
ld [wPrinterStatusFlags], a
xor a
ld [wPrinterOpcode], a
ret
.Send_0F:
ld a, $f
call .SendByte
call .NextInstruction
ret
.Send_00:
ld a, $0
call .SendByte
call .NextInstruction
ret
.SignalQuit:
ld a, $8
call .SendByte
call .NextInstruction
ret
.SendByte:
ldh [rSB], a
ld a, $1
ldh [rSC], a
ld a, $81
ldh [rSC], a
ret
.Receive2_:
ldh a, [rSB]
ld [wPrinterStatusFlags], a
xor a
ld [wPrinterOpcode], a
ret
|