|
@@ -54,7 +54,6 @@ enum PRN_CMDS {
|
54
|
54
|
};
|
55
|
55
|
|
56
|
56
|
struct prn_header {
|
57
|
|
- uint16_t magic;
|
58
|
57
|
uint8_t command;
|
59
|
58
|
uint8_t compression;
|
60
|
59
|
uint16_t length;
|
|
@@ -83,34 +82,38 @@ static uint8_t prn_send_receive(uint8_t b) {
|
83
|
82
|
static void prn_send_block(uint8_t *data, uint16_t length, uint16_t *crc) {
|
84
|
83
|
while (length-- > 0) {
|
85
|
84
|
uint8_t v = *data;
|
86
|
|
- *crc += v;
|
87
|
85
|
*data = prn_send_receive(v);
|
88
|
86
|
data++;
|
|
87
|
+
|
|
88
|
+ if (crc) {
|
|
89
|
+ *crc += v;
|
|
90
|
+ }
|
89
|
91
|
}
|
90
|
92
|
}
|
91
|
93
|
|
92
|
94
|
static enum PRN_STATUS printer_send_command(enum PRN_CMDS cmd,
|
93
|
95
|
uint8_t *data, uint16_t length) {
|
94
|
|
- static struct prn_header header;
|
95
|
|
- static struct prn_footer footer;
|
96
|
|
- uint16_t crc = 0;
|
|
96
|
+ uint16_t magic = PRN_MAGIC;
|
|
97
|
+ prn_send_block((uint8_t *)&magic, sizeof(uint16_t), NULL);
|
97
|
98
|
|
98
|
|
- header.magic = PRN_MAGIC;
|
|
99
|
+ static struct prn_header header;
|
99
|
100
|
header.command = cmd;
|
100
|
101
|
header.compression = 0;
|
101
|
102
|
header.length = data ? length : 0;
|
102
|
103
|
|
103
|
|
- footer.crc = 0;
|
104
|
|
- footer.alive = 0;
|
105
|
|
- footer.status = 0;
|
106
|
|
-
|
|
104
|
+ uint16_t crc = 0;
|
107
|
105
|
prn_send_block((uint8_t *)&header, sizeof(struct prn_header), &crc);
|
|
106
|
+
|
108
|
107
|
if (data && (length > 0)) {
|
109
|
108
|
prn_send_block(data, length, &crc);
|
110
|
109
|
}
|
111
|
110
|
|
|
111
|
+ static struct prn_footer footer;
|
112
|
112
|
footer.crc = crc;
|
113
|
|
- prn_send_block((uint8_t *)&footer, sizeof(struct prn_footer), &crc);
|
|
113
|
+ footer.alive = 0;
|
|
114
|
+ footer.status = 0;
|
|
115
|
+
|
|
116
|
+ prn_send_block((uint8_t *)&footer, sizeof(struct prn_footer), NULL);
|
114
|
117
|
|
115
|
118
|
enum PRN_STATUS r = footer.status;
|
116
|
119
|
if (footer.alive != PRN_MAGIC_DETECT) {
|
|
@@ -128,23 +131,11 @@ static enum PRN_STATUS printer_wait(uint16_t timeout, uint8_t mask, uint8_t valu
|
128
|
131
|
enum PRN_STATUS error = PRN_STATUS_OK;
|
129
|
132
|
|
130
|
133
|
while (1) {
|
131
|
|
-#ifdef DEBUG
|
132
|
|
- error = (error & 0xF000) | printer_send_command(PRN_CMD_STATUS, NULL, 0);
|
133
|
|
-#else
|
134
|
134
|
error = printer_send_command(PRN_CMD_STATUS, NULL, 0);
|
135
|
|
-#endif // DEBUG
|
136
|
135
|
if ((error & mask) == value) {
|
137
|
136
|
break;
|
138
|
137
|
}
|
139
|
138
|
|
140
|
|
-#ifdef DEBUG
|
141
|
|
- EMU_printf("%s: 0x%04x\n", __func__, (uint16_t)error);
|
142
|
|
-
|
143
|
|
- uint8_t n = (error & 0xF000) >> 12;
|
144
|
|
- n = (n + 1) & 0x000F;
|
145
|
|
- error = (error & 0x0FFF) | (n << 12);
|
146
|
|
-#endif // DEBUG
|
147
|
|
-
|
148
|
139
|
if (printer_check_cancel()) {
|
149
|
140
|
printer_send_command(PRN_CMD_BREAK, NULL, 0);
|
150
|
141
|
error |= PRN_STATUS_CANCELLED;
|
|
@@ -172,7 +163,7 @@ enum PRN_STATUS gbprinter_detect(void) BANKED {
|
172
|
163
|
#ifdef DEBUG
|
173
|
164
|
EMU_printf("%s: 0x%04x\n", __func__, (uint16_t)r);
|
174
|
165
|
#endif // DEBUG
|
175
|
|
- return r | PRN_STATUS_DETECT;
|
|
166
|
+ return r | PRN_STATUS_AT_DETECT;
|
176
|
167
|
}
|
177
|
168
|
|
178
|
169
|
static void win_str_helper(const char *s, uint8_t y_pos) {
|
|
@@ -218,12 +209,13 @@ enum PRN_STATUS gbprinter_screenshot(uint8_t win, uint8_t palette) BANKED {
|
218
|
209
|
|
219
|
210
|
r = printer_send_command(PRN_CMD_DATA, tile_buff, sizeof(tile_buff));
|
220
|
211
|
if ((r & ~PRN_STATUS_UNTRAN) != PRN_STATUS_OK) {
|
|
212
|
+ r |= PRN_STATUS_AT_DATA;
|
221
|
213
|
goto end;
|
222
|
214
|
}
|
223
|
215
|
|
224
|
216
|
if (printer_check_cancel()) {
|
225
|
217
|
printer_send_command(PRN_CMD_BREAK, NULL, 0);
|
226
|
|
- r = PRN_STATUS_CANCELLED;
|
|
218
|
+ r |= PRN_STATUS_CANCELLED;
|
227
|
219
|
goto end;
|
228
|
220
|
}
|
229
|
221
|
}
|
|
@@ -238,12 +230,14 @@ enum PRN_STATUS gbprinter_screenshot(uint8_t win, uint8_t palette) BANKED {
|
238
|
230
|
printer_send_command(PRN_CMD_PRINT, (uint8_t *)¶ms, sizeof(struct prn_config));
|
239
|
231
|
|
240
|
232
|
r = printer_wait(PRN_BUSY_TIMEOUT, PRN_STATUS_BUSY, PRN_STATUS_BUSY);
|
241
|
|
- if (r & PRN_STATUS_MASK_ERRORS) {
|
|
233
|
+ if ((r & ~(PRN_STATUS_FULL | PRN_STATUS_TIMEOUT)) & PRN_STATUS_MASK_ERRORS) {
|
|
234
|
+ r |= PRN_STATUS_AT_BUSY;
|
242
|
235
|
goto end;
|
243
|
236
|
}
|
244
|
237
|
|
245
|
238
|
r = printer_wait(PRN_PRINT_TIMEOUT, PRN_STATUS_BUSY, 0);
|
246
|
|
- if (r & PRN_STATUS_MASK_ERRORS) {
|
|
239
|
+ if ((r & ~PRN_STATUS_FULL) & PRN_STATUS_MASK_ERRORS) {
|
|
240
|
+ r |= PRN_STATUS_AT_FINAL;
|
247
|
241
|
goto end;
|
248
|
242
|
}
|
249
|
243
|
|
|
@@ -251,5 +245,5 @@ end:
|
251
|
245
|
#ifdef DEBUG
|
252
|
246
|
EMU_printf("%s: 0x%04x\n", __func__, (uint16_t)r);
|
253
|
247
|
#endif // DEBUG
|
254
|
|
- return r;
|
|
248
|
+ return (r & ~PRN_STATUS_FULL);
|
255
|
249
|
}
|