-
Notifications
You must be signed in to change notification settings - Fork 0
/
printer.inc
274 lines (235 loc) · 7.21 KB
/
printer.inc
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
; GameBoy printer access
; thanks to "ClapOn/ClapOff" I know how to program the printer...
; send byte and wait for result
send_byte:
ldh (SB & 0FFh),a
ld a,081h ; start transfer
ldh (SC & 0FFh),a
sb_l1:
ldh a,(SC & 0FFh) ; check status
and 080h
jr nz, sb_l1
ldh a,(SB & 0FFh)
ret
; open packet and clear CRC
prn_open:
xor a ; clear CRC
ldh (crc & 0FFh), a
ldh ((crc+1) & 0FFh), a
ld a, 088h ; access printer
call send_byte
ld a, 033h
call send_byte
ret
; write CRC and get status
prn_close:
ldh a,(crc & 0FFh)
call send_byte
ldh a,((crc+1) & 0FFh)
call send_byte
xor a
call send_byte
ldh (stat_1 & 0FFh),a
xor a
call send_byte
ldh (stat_2 & 0FFh),a
ret
; send "bc" bytes starting from "hl" to printer and update CRC
prn_send:
ldi a,(hl) ; get byte from string
ld e,a ; add to CRC
ldh a,(crc & 0FFh)
add a,e
ldh (crc & 0FFh),a
ldh a,((crc+1) & 0FFh)
adc a,0
ldh ((crc+1) & 0FFh),a
ld a,e
call send_byte
dec bc
ld a,b
or c
jr nz, prn_send
ret
; Prepare printer for printing
prn_init:
call prn_open
ld hl, cmd_init
ld bc, 4
call prn_send
call prn_close
ret
; Tell the printer to print the buffer
prn_start:
call prn_open
ld hl, cmd_start
ld bc, 8
call prn_send
call prn_close
ret
; Ask printer for status
prn_status:
call prn_open
ld hl, cmd_status
ld bc, 4
call prn_send
call prn_close
ret
; Send last packet of data to printer
prn_last:
call prn_open
ld hl, cmd_eod
ld bc, 4
call prn_send
call prn_close
ret
; send a memory-buffer to the printer and print it, hl=32*18 memory buffer
prn_prtbuf:
push hl
; first display the status-window
ld hl, 09C00h
ld b,20-4
ld c,6-4
call draw_border
ld a, 7
ldh (WX & 0FFh),a
ld a, 96
ldh (WY & 0FFh),a
ldh a,(LCDC & 0FFh)
set 5,a
res 1,a
ldh (LCDC & 0FFh),a
ld hl, prn_con
ld de, 09C00h + 1 + (2*32)
call copy_z
ld hl, prn_none
ld de, 09C00h + 1 + (3*32)
call copy_z
call prn_init
pop hl
ldh a,(stat_1 & 0FFh) ; check if the printer responded
and 0f0h
cp 080h
jp nz, no_ack
ldh a,(stat_2 & 0FFh) ; exit, if an error is found
and 0f0h
jp nz, p_error
ld c,1
pp_l2: push bc
push hl
; display status-bar
ld b,7
ld hl, 09C00h + 1 + (3*32)
pp_l4:
ldh a,(STAT & 0FFh)
bit 1,a
jr nz,pp_l4
ld a, 'O'
ldi (hl),a
dec b
dec c
jr nz, pp_l4
pp_l5:
ldh a,(STAT & 0FFh)
bit 1,a
jr nz,pp_l5
ld a, '-'
ldi (hl),a
dec b
jr nz, pp_l5
call prn_open
ld hl,cmd_2row
ld bc,4
call prn_send
pop hl
ld b,2
pp_l3: ld c,20
pp_l1: push bc
ldi a,(hl)
push hl
ld l,a
ld h,0
add hl,hl ; hl*=16
add hl,hl
add hl,hl
add hl,hl
ld de, text_font
add hl,de
ld bc,16
call prn_send
pop hl
pop bc
dec c
jr nz,pp_l1
ld de,12
add hl,de
dec b
jr nz,pp_l3
push hl
call prn_close
pop hl
pop bc
ldh a,(stat_1 & 0FFh) ; check if the printer responded
and 0f0h
cp 080h
jp nz, no_ack
ldh a,(stat_2 & 0FFh) ; exit, if an error is found
and 0f0h
jp nz, p_error
inc c
ld a,c
cp 7
jr nz,pp_l2
call prn_last
call prn_start
done_print: ; hide status-window
ldh a,(LCDC & 0FFh)
res 5,a
set 1,a
ldh (LCDC & 0FFh),a
ret
; error condition:
no_ack:
ld hl, prn_link
ld de, 09C00h + 1 + (3*32)
call copy_z
call wait_key
jp done_print
p_error:
ld de, 09C00h + 1 + (3*32)
ld hl, prn_bat
cp 080h
call nz, copy_z
ld hl, prn_temp
cp 040h
call nz, copy_z
ld hl, prn_jam
cp 020h
call nz, copy_z
call wait_key
jp done_print
; A few codes:
cmd_init:
.byte 001h ; Print Init Command
.byte 000h
.word 0
cmd_eod:
.byte 004h ; Data Packet Command
.byte 000h
.word 0
cmd_status:
.byte 00fh ; Status Check Command
.byte 000h
.word 0
cmd_start:
.byte 002h ; Start Print Command
.byte 000h
.word 4 ; 4 bytes data
.byte 001h
.byte 013h
.byte 0e4h
.byte 040h
cmd_2row:
.byte 004h ; Data Packet Command
.byte 000h
.word 640 ; 640 bytes data