Browse Source

fix wrong gbprinter crc and use proper status flag checks

Thomas B 3 weeks ago
parent
commit
76cbbd75cc
4 changed files with 69 additions and 65 deletions
  1. 22
    28
      src/gbprinter.c
  2. 19
    15
      src/gbprinter.h
  3. 20
    16
      src/gbprinter_error.c
  4. 8
    6
      src/main.c

+ 22
- 28
src/gbprinter.c View File

@@ -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 *)&params, 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
 }

+ 19
- 15
src/gbprinter.h View File

@@ -27,28 +27,32 @@
27 27
 #include <stdint.h>
28 28
 
29 29
 enum PRN_STATUS {
30
+    PRN_STATUS_OK          = 0x0000, // everything is fine
31
+
30 32
     // status flags from printer
31
-    PRN_STATUS_LOWBAT      = 0x80, // battery too low
32
-    PRN_STATUS_ER2         = 0x40, // unspecified error
33
-    PRN_STATUS_ER1         = 0x20, // paper jam
34
-    PRN_STATUS_ER0         = 0x10, // packet error
35
-    PRN_STATUS_UNTRAN      = 0x08, // unprinted data in buffer
36
-    PRN_STATUS_FULL        = 0x04, // ready, triggered by DATA with len 0
37
-    PRN_STATUS_BUSY        = 0x02, // printer is printing
38
-    PRN_STATUS_CHECKSUM    = 0x01, // checksum error
39
-    PRN_STATUS_OK          = 0x00, // everything is fine
33
+    PRN_STATUS_CHECKSUM    = 0x0001, // checksum error
34
+    PRN_STATUS_BUSY        = 0x0002, // printer is printing
35
+    PRN_STATUS_FULL        = 0x0004, // ready, triggered by DATA with len 0
36
+    PRN_STATUS_UNTRAN      = 0x0008, // unprinted data in buffer
37
+    PRN_STATUS_ER0         = 0x0010, // packet error
38
+    PRN_STATUS_ER1         = 0x0020, // paper jam
39
+    PRN_STATUS_ER2         = 0x0040, // unspecified error
40
+    PRN_STATUS_LOWBAT      = 0x0080, // battery too low
40 41
 
41 42
     // status flags from driver code
42
-    PRN_STATUS_CANCELLED   = 0x100, // user has aborted the print by pressing B
43
-    PRN_STATUS_TIMEOUT     = 0x200, // timeout waiting for printer response
44
-    PRN_STATUS_NO_MAGIC    = 0x400, // printer did not respond with proper 'alive'
43
+    PRN_STATUS_CANCELLED   = 0x0100, // user has aborted the print by pressing B
44
+    PRN_STATUS_TIMEOUT     = 0x0200, // timeout waiting for printer response
45
+    PRN_STATUS_NO_MAGIC    = 0x0400, // printer did not respond with proper 'alive'
45 46
 
46 47
     // status flags for user code
47
-    PRN_STATUS_DETECT      = 0x800, // set in gbprinter_detect
48
+    PRN_STATUS_AT_DETECT   = 0x0800, // set in gbprinter_detect
49
+    PRN_STATUS_AT_DATA     = 0x1000, // set at abort on tile data
50
+    PRN_STATUS_AT_BUSY     = 0x2000, // set at abort on busy wait print
51
+    PRN_STATUS_AT_FINAL    = 0x4000, // set at abort on final wait
48 52
 
49 53
     // masks to check for errors
50
-    PRN_STATUS_MASK_ERRORS = 0x7F0,
51
-    PRN_STATUS_MASK_ANY    = 0x7FF,
54
+    PRN_STATUS_MASK_ERRORS = 0x07F0,
55
+    PRN_STATUS_MASK_ANY    = 0x07FF,
52 56
 };
53 57
 
54 58
 #define PRN_PALETTE_NORMAL 0b11100100u

+ 20
- 16
src/gbprinter_error.c View File

@@ -29,27 +29,31 @@
29 29
 
30 30
 BANKREF(gbprinter_error)
31 31
 
32
-#define ERROR_BITS 12
32
+#define ERROR_BITS 15
33 33
 
34 34
 static const char str_ok[] = "ok";
35 35
 
36
-static const char str_lowbat[] = "battery too low";
37
-static const char str_er2[] = "unknown error";
38
-static const char str_er1[] = "paper jam";
39
-static const char str_er0[] = "packet error";
40
-static const char str_untran[] = "unprocessed";
41
-static const char str_full[] = "data full";
42
-static const char str_busy[] = "printer busy";
43
-static const char str_sum[] = "checksum error";
44
-static const char str_cancel[] = "cancelled";
45
-static const char str_timeout[] = "timeout";
46
-static const char str_magic[] = "wrong magic byte";
47
-static const char str_detect[] = "@ detection";
36
+static const char       str_sum[] = "checksum error";
37
+static const char      str_busy[] = "printer busy";
38
+static const char      str_full[] = "data full";
39
+static const char    str_untran[] = "unprocessed";
40
+static const char       str_er0[] = "packet error";
41
+static const char       str_er1[] = "paper jam";
42
+static const char       str_er2[] = "unknown error";
43
+static const char    str_lowbat[] = "battery too low";
44
+static const char    str_cancel[] = "cancelled";
45
+static const char   str_timeout[] = "timeout";
46
+static const char     str_magic[] = "wrong magic byte";
47
+static const char str_at_detect[] = "@ detection";
48
+static const char   str_at_data[] = "@ tile data";
49
+static const char   str_at_busy[] = "@ busy wait";
50
+static const char  str_at_final[] = "@ final wait";
48 51
 
49 52
 static const char * const error_strings[ERROR_BITS] = {
50
-    str_sum, str_busy, str_full, str_untran,
51
-    str_er0, str_er1, str_er2, str_lowbat,
52
-    str_cancel, str_timeout, str_magic, str_detect,
53
+    str_sum,     str_busy,    str_full,     str_untran,
54
+    str_er0,     str_er1,     str_er2,      str_lowbat,
55
+    str_cancel,  str_timeout, str_magic,    str_at_detect,
56
+    str_at_data, str_at_busy, str_at_final,
53 57
 };
54 58
 
55 59
 uint8_t gbprinter_error(enum PRN_STATUS status, char *buff) BANKED {

+ 8
- 6
src/main.c View File

@@ -100,12 +100,14 @@ static void highscore(uint8_t is_black) NONBANKED {
100 100
                 status = gbprinter_screenshot(1, is_black ? PRN_PALETTE_SC_B : PRN_PALETTE_SC_W);
101 101
             }
102 102
 
103
-            win_score_clear(2, 0);
104
-            win_score_print(status);
105
-            while (1) {
106
-                key_read();
107
-                if (key_pressed(0xFF)) break;
108
-                vsync();
103
+            if (status != PRN_STATUS_OK) {
104
+                win_score_clear(2, 0);
105
+                win_score_print(status);
106
+                while (1) {
107
+                    key_read();
108
+                    if (key_pressed(0xFF)) break;
109
+                    vsync();
110
+                }
109 111
             }
110 112
 
111 113
             break;

Loading…
Cancel
Save