|
@@ -0,0 +1,366 @@
|
|
1
|
+/*
|
|
2
|
+ * gbprinter.c
|
|
3
|
+ * Duality
|
|
4
|
+ *
|
|
5
|
+ * Based on the gbprinter example from gbdk-2020:
|
|
6
|
+ * https://github.com/gbdk-2020/gbdk-2020/tree/develop/gbdk-lib/examples/gb/gbprinter
|
|
7
|
+ *
|
|
8
|
+ * Copyright (C) 2025 Thomas Buck <thomas@xythobuz.de>
|
|
9
|
+ *
|
|
10
|
+ * This program is free software: you can redistribute it and/or modify
|
|
11
|
+ * it under the terms of the GNU General Public License as published by
|
|
12
|
+ * the Free Software Foundation, either version 3 of the License, or
|
|
13
|
+ * (at your option) any later version.
|
|
14
|
+ *
|
|
15
|
+ * This program is distributed in the hope that it will be useful,
|
|
16
|
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
18
|
+ * GNU General Public License for more details.
|
|
19
|
+ *
|
|
20
|
+ * See <http://www.gnu.org/licenses/>.
|
|
21
|
+ */
|
|
22
|
+
|
|
23
|
+#include <gbdk/platform.h>
|
|
24
|
+#include <gbdk/emu_debug.h>
|
|
25
|
+#include <stdio.h>
|
|
26
|
+#include <string.h>
|
|
27
|
+
|
|
28
|
+#include "input.h"
|
|
29
|
+#include "gbprinter.h"
|
|
30
|
+
|
|
31
|
+BANKREF(gbprinter)
|
|
32
|
+
|
|
33
|
+/** Width of the printed image in tiles
|
|
34
|
+*/
|
|
35
|
+#define PRN_TILE_WIDTH 20
|
|
36
|
+
|
|
37
|
+#define PRN_LOW(A) ((A) & 0xFF)
|
|
38
|
+#define PRN_HIGH(A) ((A) >> 8)
|
|
39
|
+
|
|
40
|
+/** 0x88,0x33 are mandatory first bytes to initialise a communication with printer
|
|
41
|
+ * Any command sequence begins by these
|
|
42
|
+ */
|
|
43
|
+#define PRN_MAGIC 0x3388
|
|
44
|
+#define PRN_LE(A) PRN_LOW(A),PRN_HIGH(A)
|
|
45
|
+
|
|
46
|
+/** magic number that is sent in the reply packet by the printer before the status byte
|
|
47
|
+ */
|
|
48
|
+#define PRN_MAGIC_DETECT 0x81
|
|
49
|
+
|
|
50
|
+/** INIT command is mandatory to initialize communication protocol with the printer
|
|
51
|
+ * Two consecutive linked commands must never be more than 150 ms apart except the INIT command which is valid at least 10 seconds
|
|
52
|
+ */
|
|
53
|
+#define PRN_CMD_INIT 0x01
|
|
54
|
+
|
|
55
|
+/** PRINT command
|
|
56
|
+ * Contains the palette, margins, number of prints and printing intensity
|
|
57
|
+ */
|
|
58
|
+#define PRN_CMD_PRINT 0x02
|
|
59
|
+
|
|
60
|
+/** DATA command
|
|
61
|
+ * Can be any length between 0 and 640 bytes.
|
|
62
|
+ * DATA command with lenght 0 triggers PRN_STATUS_FULL and is mandatory before print command
|
|
63
|
+ */
|
|
64
|
+#define PRN_CMD_DATA 0x04
|
|
65
|
+
|
|
66
|
+/** BREAK command
|
|
67
|
+ * Not very usefull but exists (see Game Boy Programming Manual)
|
|
68
|
+ */
|
|
69
|
+#define PRN_CMD_BREAK 0x08
|
|
70
|
+
|
|
71
|
+/** STATUS command
|
|
72
|
+ * Used to check status bits
|
|
73
|
+ * Maybe be used alone before an INIT command to check physical connection with printer
|
|
74
|
+ * Resets PRN_STATUS_UNTRAN
|
|
75
|
+ */
|
|
76
|
+#define PRN_CMD_STATUS 0x0F
|
|
77
|
+
|
|
78
|
+/** Palette format: the bits, grouped two by two, give the printing color of the encoded pixel value
|
|
79
|
+ * for the default palette 0xE4 = 0b11100100 = [3 2 1 0]
|
|
80
|
+ * Any value is valid, which means that 1 to 4 color images are possible
|
|
81
|
+ * 0x00 acts the same as 0xE4 for the printer
|
|
82
|
+ */
|
|
83
|
+#define PRN_PALETTE_NORMAL 0b11100100u
|
|
84
|
+#define PRN_PALETTE_INV 0b00011011u
|
|
85
|
+
|
|
86
|
+/** Don't use margins
|
|
87
|
+ */
|
|
88
|
+#define PRN_NO_MARGINS 0x00
|
|
89
|
+
|
|
90
|
+/** Exposure: 0x40 is default value, values from 0x80 to 0xFF act as 0x40
|
|
91
|
+ * Determines the time used by the printer head to heat the thermal paper
|
|
92
|
+ */
|
|
93
|
+#define PRN_EXPOSURE_LIGHT 0x00
|
|
94
|
+#define PRN_EXPOSURE_DEFAULT 0x40
|
|
95
|
+#define PRN_EXPOSURE_DARK 0x7F
|
|
96
|
+
|
|
97
|
+/** Battery too low
|
|
98
|
+ */
|
|
99
|
+#define PRN_STATUS_LOWBAT 0x80
|
|
100
|
+
|
|
101
|
+/** Error not specified according to the Game Boy Programming manual
|
|
102
|
+ */
|
|
103
|
+#define PRN_STATUS_ER2 0x40
|
|
104
|
+
|
|
105
|
+/** Paper jam (abnormal motor operation)
|
|
106
|
+ */
|
|
107
|
+#define PRN_STATUS_ER1 0x20
|
|
108
|
+
|
|
109
|
+/** Packet error (but not checksum error)
|
|
110
|
+ */
|
|
111
|
+#define PRN_STATUS_ER0 0x10
|
|
112
|
+
|
|
113
|
+/** Unprocessed data present in printer memory
|
|
114
|
+ * Allows to verify that printer got some data in memory with correct checksum
|
|
115
|
+ * is resetted by STATUS command
|
|
116
|
+ */
|
|
117
|
+#define PRN_STATUS_UNTRAN 0x08
|
|
118
|
+
|
|
119
|
+/** status data ready, mandatory to allow printing
|
|
120
|
+ * is triggered by DATA command with lenght 0
|
|
121
|
+ */
|
|
122
|
+#define PRN_STATUS_FULL 0x04
|
|
123
|
+
|
|
124
|
+/** Message sent by the printer while physically printing
|
|
125
|
+ */
|
|
126
|
+#define PRN_STATUS_BUSY 0x02
|
|
127
|
+
|
|
128
|
+/** The received packet has a ckecksum error
|
|
129
|
+ */
|
|
130
|
+#define PRN_STATUS_SUM 0x01
|
|
131
|
+
|
|
132
|
+#define PRN_STATUS_MASK_ERRORS 0xF0
|
|
133
|
+#define PRN_STATUS_MASK_ANY 0xFF
|
|
134
|
+
|
|
135
|
+#define PRN_SECONDS(A) ((A)*60)
|
|
136
|
+
|
|
137
|
+#define PRN_MAX_PROGRESS 8
|
|
138
|
+
|
|
139
|
+#define PRN_STATUS_CANCELLED PRN_STATUS_ER2
|
|
140
|
+
|
|
141
|
+#define REINIT_SEIKO
|
|
142
|
+
|
|
143
|
+#define START_TRANSFER 0x81
|
|
144
|
+#define PRN_BUSY_TIMEOUT PRN_SECONDS(2)
|
|
145
|
+#define PRN_COMPLETION_TIMEOUT PRN_SECONDS(20)
|
|
146
|
+#define PRN_SEIKO_RESET_TIMEOUT 10
|
|
147
|
+
|
|
148
|
+#define PRN_FINAL_MARGIN 0x03
|
|
149
|
+
|
|
150
|
+typedef struct start_print_pkt_s {
|
|
151
|
+ uint16_t magic;
|
|
152
|
+ uint16_t command;
|
|
153
|
+ uint16_t length;
|
|
154
|
+ uint8_t print;
|
|
155
|
+ uint8_t margins;
|
|
156
|
+ uint8_t palette;
|
|
157
|
+ uint8_t exposure;
|
|
158
|
+ uint16_t crc;
|
|
159
|
+ uint16_t trail;
|
|
160
|
+} start_print_pkt_t;
|
|
161
|
+
|
|
162
|
+static const uint8_t PRN_PKT_INIT[] = {
|
|
163
|
+ PRN_LE(PRN_MAGIC), PRN_LE(PRN_CMD_INIT), PRN_LE(0), PRN_LE(0x01), PRN_LE(0)
|
|
164
|
+};
|
|
165
|
+
|
|
166
|
+static const uint8_t PRN_PKT_STATUS[] = {
|
|
167
|
+ PRN_LE(PRN_MAGIC), PRN_LE(PRN_CMD_STATUS), PRN_LE(0), PRN_LE(0x0F), PRN_LE(0)
|
|
168
|
+};
|
|
169
|
+
|
|
170
|
+static const uint8_t PRN_PKT_EOF[] = {
|
|
171
|
+ PRN_LE(PRN_MAGIC), PRN_LE(PRN_CMD_DATA), PRN_LE(0), PRN_LE(0x04), PRN_LE(0)
|
|
172
|
+};
|
|
173
|
+
|
|
174
|
+static const uint8_t PRN_PKT_CANCEL[] = {
|
|
175
|
+ PRN_LE(PRN_MAGIC), PRN_LE(PRN_CMD_BREAK), PRN_LE(0), PRN_LE(0x01), PRN_LE(0)
|
|
176
|
+};
|
|
177
|
+
|
|
178
|
+static start_print_pkt_t PRN_PKT_START = {
|
|
179
|
+ .magic = PRN_MAGIC, .command = PRN_CMD_PRINT,
|
|
180
|
+ .length = 4, .print = TRUE,
|
|
181
|
+ .margins = 0, .palette = PRN_PALETTE_NORMAL, .exposure = PRN_EXPOSURE_DARK,
|
|
182
|
+ .crc = 0, .trail = 0
|
|
183
|
+};
|
|
184
|
+
|
|
185
|
+static uint16_t printer_status;
|
|
186
|
+static uint8_t printer_tile_num;
|
|
187
|
+
|
|
188
|
+static inline void gbprinter_set_print_params(uint8_t margins, uint8_t palette, uint8_t exposure) {
|
|
189
|
+ PRN_PKT_START.crc = ((PRN_CMD_PRINT + 0x04u + 0x01u)
|
|
190
|
+ + (PRN_PKT_START.margins = margins)
|
|
191
|
+ + (PRN_PKT_START.palette = palette)
|
|
192
|
+ + (PRN_PKT_START.exposure = exposure));
|
|
193
|
+}
|
|
194
|
+
|
|
195
|
+static uint8_t printer_send_receive(uint8_t b) {
|
|
196
|
+ SB_REG = b;
|
|
197
|
+ SC_REG = START_TRANSFER;
|
|
198
|
+ while (SC_REG & 0x80);
|
|
199
|
+ return SB_REG;
|
|
200
|
+}
|
|
201
|
+
|
|
202
|
+static uint8_t printer_send_byte(uint8_t b) {
|
|
203
|
+ return (uint8_t)(printer_status = ((printer_status << 8) | printer_send_receive(b)));
|
|
204
|
+}
|
|
205
|
+
|
|
206
|
+static uint8_t printer_send_command(const uint8_t *command, uint8_t length) {
|
|
207
|
+ uint8_t index = 0;
|
|
208
|
+ while (index++ < length) printer_send_byte(*command++);
|
|
209
|
+ return ((uint8_t)(printer_status >> 8) == PRN_MAGIC_DETECT) ? (uint8_t)printer_status : PRN_STATUS_MASK_ERRORS;
|
|
210
|
+}
|
|
211
|
+
|
|
212
|
+#define PRINTER_SEND_COMMAND(CMD) printer_send_command((const uint8_t *)&(CMD), sizeof(CMD))
|
|
213
|
+
|
|
214
|
+static uint8_t printer_print_tile(const uint8_t *tiledata) {
|
|
215
|
+ static const uint8_t PRINT_TILE[] = { 0x88,0x33,0x04,0x00,0x80,0x02 };
|
|
216
|
+ static uint16_t printer_CRC;
|
|
217
|
+ if (printer_tile_num == 0) {
|
|
218
|
+ const uint8_t * data = PRINT_TILE;
|
|
219
|
+ for (uint8_t i = sizeof(PRINT_TILE); i != 0; i--) printer_send_receive(*data++);
|
|
220
|
+ printer_CRC = 0x04 + 0x80 + 0x02;
|
|
221
|
+ }
|
|
222
|
+ for(uint8_t i = 0x10; i != 0; i--, tiledata++) {
|
|
223
|
+ printer_CRC += *tiledata;
|
|
224
|
+ printer_send_receive(*tiledata);
|
|
225
|
+ }
|
|
226
|
+ if (++printer_tile_num == 40) {
|
|
227
|
+ printer_send_receive((uint8_t)printer_CRC);
|
|
228
|
+ printer_send_receive((uint8_t)(printer_CRC >> 8));
|
|
229
|
+ printer_send_receive(0x00);
|
|
230
|
+ printer_send_receive(0x00);
|
|
231
|
+ printer_CRC = printer_tile_num = 0;
|
|
232
|
+ return TRUE;
|
|
233
|
+ }
|
|
234
|
+ return FALSE;
|
|
235
|
+}
|
|
236
|
+
|
|
237
|
+static inline void printer_init(void) {
|
|
238
|
+ printer_tile_num = 0;
|
|
239
|
+ PRINTER_SEND_COMMAND(PRN_PKT_INIT);
|
|
240
|
+}
|
|
241
|
+
|
|
242
|
+uint8_t printer_check_cancel(void) {
|
|
243
|
+ key_read();
|
|
244
|
+ return key_pressed(J_B);
|
|
245
|
+}
|
|
246
|
+
|
|
247
|
+static uint8_t printer_wait(uint16_t timeout, uint8_t mask, uint8_t value) {
|
|
248
|
+ uint8_t error;
|
|
249
|
+ while (((error = PRINTER_SEND_COMMAND(PRN_PKT_STATUS)) & mask) != value) {
|
|
250
|
+ if (printer_check_cancel()) {
|
|
251
|
+ PRINTER_SEND_COMMAND(PRN_PKT_CANCEL);
|
|
252
|
+ return PRN_STATUS_CANCELLED;
|
|
253
|
+ }
|
|
254
|
+ if (timeout-- == 0) return PRN_STATUS_MASK_ERRORS;
|
|
255
|
+ if (error & PRN_STATUS_MASK_ERRORS) break;
|
|
256
|
+ vsync();
|
|
257
|
+ }
|
|
258
|
+ return error;
|
|
259
|
+}
|
|
260
|
+
|
|
261
|
+uint8_t gbprinter_detect(uint8_t delay) BANKED {
|
|
262
|
+ printer_init();
|
|
263
|
+ uint8_t r = printer_wait(delay, PRN_STATUS_MASK_ANY, PRN_STATUS_OK);
|
|
264
|
+#ifdef DEBUG
|
|
265
|
+ EMU_printf("%s: %hu\n", __func__, (uint8_t)r);
|
|
266
|
+#endif // DEBUG
|
|
267
|
+ return r;
|
|
268
|
+}
|
|
269
|
+
|
|
270
|
+uint8_t gbprinter_print_image(const uint8_t *image_map, const uint8_t *image,
|
|
271
|
+ int8_t pos_x, uint8_t width, uint8_t height) BANKED {
|
|
272
|
+ uint8_t tile_data[16];
|
|
273
|
+ uint8_t rows = ((height + 1) >> 1) << 1;
|
|
274
|
+ uint8_t pkt_count = 0;
|
|
275
|
+
|
|
276
|
+ if ((rows >> 1) == 0) return PRN_STATUS_OK;
|
|
277
|
+
|
|
278
|
+ printer_tile_num = 0;
|
|
279
|
+
|
|
280
|
+ for (uint8_t y = 0; y < rows; y++) {
|
|
281
|
+ for (int16_t x = 0; x < PRN_TILE_WIDTH; x++) {
|
|
282
|
+#ifdef DEBUG
|
|
283
|
+ EMU_printf("%s: %hu %i\n", __func__, (uint8_t)y, (int16_t)x);
|
|
284
|
+#endif // DEBUG
|
|
285
|
+
|
|
286
|
+ // overlay the picture tile if in range
|
|
287
|
+ if ((y < height) && (x >= pos_x) && (x < (pos_x + width))) {
|
|
288
|
+ uint8_t tile = image_map[(y * width) + (x - pos_x)];
|
|
289
|
+ memcpy(tile_data, image + ((uint16_t)tile << 4), sizeof(tile_data));
|
|
290
|
+ } else {
|
|
291
|
+ memset(tile_data, 0, sizeof(tile_data));
|
|
292
|
+ }
|
|
293
|
+
|
|
294
|
+ // print the resulting tile
|
|
295
|
+ if (printer_print_tile(tile_data)) {
|
|
296
|
+ pkt_count++;
|
|
297
|
+
|
|
298
|
+ if (printer_check_cancel()) {
|
|
299
|
+ PRINTER_SEND_COMMAND(PRN_PKT_CANCEL);
|
|
300
|
+ return PRN_STATUS_CANCELLED;
|
|
301
|
+ }
|
|
302
|
+ }
|
|
303
|
+
|
|
304
|
+ if (pkt_count == 9) {
|
|
305
|
+ pkt_count = 0;
|
|
306
|
+ PRINTER_SEND_COMMAND(PRN_PKT_EOF);
|
|
307
|
+
|
|
308
|
+ // setup margin if last packet
|
|
309
|
+ gbprinter_set_print_params((y == (rows - 1)) ? PRN_FINAL_MARGIN : PRN_NO_MARGINS,
|
|
310
|
+ PRN_PALETTE_NORMAL, PRN_EXPOSURE_DARK);
|
|
311
|
+
|
|
312
|
+ PRINTER_SEND_COMMAND(PRN_PKT_START);
|
|
313
|
+
|
|
314
|
+ // query printer status
|
|
315
|
+ uint8_t error = printer_wait(PRN_BUSY_TIMEOUT, PRN_STATUS_BUSY, PRN_STATUS_BUSY);
|
|
316
|
+ if (error & PRN_STATUS_MASK_ERRORS) {
|
|
317
|
+ return error;
|
|
318
|
+ }
|
|
319
|
+
|
|
320
|
+ error = printer_wait(PRN_COMPLETION_TIMEOUT, PRN_STATUS_BUSY, 0);
|
|
321
|
+ if (error & PRN_STATUS_MASK_ERRORS) {
|
|
322
|
+ return error;
|
|
323
|
+ }
|
|
324
|
+
|
|
325
|
+#ifdef REINIT_SEIKO
|
|
326
|
+ // reinit printer (required by Seiko?)
|
|
327
|
+ if (y < (rows - 1)) {
|
|
328
|
+ PRINTER_SEND_COMMAND(PRN_PKT_INIT);
|
|
329
|
+ error = printer_wait(PRN_SEIKO_RESET_TIMEOUT, PRN_STATUS_MASK_ANY, PRN_STATUS_OK);
|
|
330
|
+ if (error) {
|
|
331
|
+ return error;
|
|
332
|
+ }
|
|
333
|
+ }
|
|
334
|
+#endif
|
|
335
|
+ }
|
|
336
|
+ }
|
|
337
|
+ }
|
|
338
|
+
|
|
339
|
+ if (pkt_count) {
|
|
340
|
+ PRINTER_SEND_COMMAND(PRN_PKT_EOF);
|
|
341
|
+
|
|
342
|
+ // setup printing if required
|
|
343
|
+ gbprinter_set_print_params(PRN_FINAL_MARGIN, PRN_PALETTE_NORMAL, PRN_EXPOSURE_DARK);
|
|
344
|
+ PRINTER_SEND_COMMAND(PRN_PKT_START);
|
|
345
|
+
|
|
346
|
+ // query printer status
|
|
347
|
+ uint8_t error = printer_wait(PRN_BUSY_TIMEOUT, PRN_STATUS_BUSY, PRN_STATUS_BUSY);
|
|
348
|
+ if (error & PRN_STATUS_MASK_ERRORS) {
|
|
349
|
+ return error;
|
|
350
|
+ }
|
|
351
|
+
|
|
352
|
+ error = printer_wait(PRN_COMPLETION_TIMEOUT, PRN_STATUS_BUSY, 0);
|
|
353
|
+ if (error & PRN_STATUS_MASK_ERRORS) {
|
|
354
|
+ return error;
|
|
355
|
+ }
|
|
356
|
+ }
|
|
357
|
+
|
|
358
|
+ return PRINTER_SEND_COMMAND(PRN_PKT_STATUS);
|
|
359
|
+}
|
|
360
|
+
|
|
361
|
+uint8_t gbprinter_screenshot(void) BANKED {
|
|
362
|
+ return gbprinter_print_image(
|
|
363
|
+ (const uint8_t *)0x9C00, (const uint8_t *)0x8800,
|
|
364
|
+ 0,
|
|
365
|
+ DEVICE_SCREEN_WIDTH, DEVICE_SCREEN_HEIGHT);
|
|
366
|
+}
|