/* QR codes on screen, so a phone camera can carry the report off the console when the * flash cart's save does not reach the SD card. One size only: version 20 (97 modules), * error correction M, byte mode, 666 bytes a code, two pixels a module. Written from the * QR specification's structure (ISO/IEC 18004); no multiply or divide, as nothing of * libgcc is linked: Galois field products go through log tables, and the few remainders * through small tables. */ #include "md.h" #include "qr.h" #define N 97 /* modules a side */ #define RAW 1085 /* codewords in version 20 */ #define ECC 26 /* error correction codewords a block, level M */ #define BLOCKS 16 /* 3 of 41 data codewords, 13 of 42 */ #define SHORT_BLOCKS 3 #define SHORT_DATA 41 #define DATA_CW 669 /* Scratch: the matrix sits in free work RAM above the save's shadow, the codewords in the * test records' area, which the report no longer needs. */ #ifdef QR_HOST static u8 host_m[N * N], host_cw[0x1000]; #define M host_m #define CW host_cw #else #define M ((u8 *)0xFFC000) /* bit 0 dark, bit 1 function module */ #define CW ((u8 *)0xFF0000) /* data 669, error correction 416, then the interleaved 1085 */ #endif #define OUT (CW + 0x600) static u8 gexp[512], glog[256], gen[ECC + 1]; static u8 *row[N]; static u8 mod3[N], div3[N]; static void tables(void) { int i, x = 1, j; for (i = 0; i < 255; i++) { gexp[i] = gexp[i + 255] = x; glog[x] = i; x <<= 1; if (x & 0x100) x ^= 0x11D; } /* generator: (x - a^0)(x - a^1)...(x - a^25), highest coefficient first, gen[0] = 1 */ for (i = 0; i <= ECC; i++) gen[i] = 0; gen[0] = 1; for (i = 0; i < ECC; i++) for (j = i + 1; j > 0; j--) gen[j] ^= gen[j - 1] ? gexp[glog[gen[j - 1]] + i] : 0; x = 0; for (i = 0; i < N; i++) row[i] = M + x, x += N; for (i = 0, x = 0, j = 0; i < N; i++) { mod3[i] = x; div3[i] = j; if (++x == 3) x = 0, j++; } } static void ecc(const u8 *data, int len, u8 *out) { int i, j; for (i = 0; i < ECC; i++) out[i] = 0; for (i = 0; i < len; i++) { u8 f = data[i] ^ out[0]; for (j = 0; j < ECC - 1; j++) out[j] = out[j + 1] ^ (f && gen[j + 1] ? gexp[glog[f] + glog[gen[j + 1]]] : 0); out[ECC - 1] = f && gen[ECC] ? gexp[glog[f] + glog[gen[ECC]]] : 0; } } static void fn(int x, int y, int dark) { row[y][x] = 2 | (dark != 0); } static void finder(int cx, int cy) { int dx, dy; for (dy = -4; dy <= 4; dy++) for (dx = -4; dx <= 4; dx++) { int x = cx + dx, y = cy + dy, d; int ax = dx < 0 ? -dx : dx, ay = dy < 0 ? -dy : dy; if (x < 0 || y < 0 || x >= N || y >= N) continue; d = ax > ay ? ax : ay; fn(x, y, d != 2 && d != 4); } } static void align(int cx, int cy) { int dx, dy; for (dy = -2; dy <= 2; dy++) for (dx = -2; dx <= 2; dx++) { int ax = dx < 0 ? -dx : dx, ay = dy < 0 ? -dy : dy; fn(cx + dx, cy + dy, (ax > ay ? ax : ay) != 1); } } static int masked(int mask, int x, int y) { int a; switch (mask) { case 0: return !((x + y) & 1); case 1: return !(y & 1); case 2: return !mod3[x]; case 3: a = x + y; while (a >= 3) a -= 3; return !a; case 4: return !((div3[x] + (y >> 1)) & 1); case 5: a = mod3[x] && mod3[y] ? (mod3[x] == mod3[y] ? 1 : 2) : 0; return !((x & y & 1) + a); case 6: a = mod3[x] && mod3[y] ? (mod3[x] == mod3[y] ? 1 : 2) : 0; return !(((x & y & 1) + a) & 1); default: a = mod3[x] && mod3[y] ? (mod3[x] == mod3[y] ? 1 : 2) : 0; return !((((x + y) & 1) + a) & 1); } } static void build(const u8 *payload, int len, int mask) { static const u8 centres[] = { 6, 34, 62, 90 }; u8 *d = CW; int i, j, k, bits, at; u32 v; /* the bit stream: byte mode, a 16-bit count, the bytes, a terminator, pad bytes */ for (i = 0; i < DATA_CW; i++) d[i] = 0; d[0] = 0x40 | (len >> 12 & 0x0F); d[1] = len >> 4; d[2] = (len & 0x0F) << 4; for (i = 0; i < len; i++) { d[2 + i] |= payload[i] >> 4; d[3 + i] = payload[i] << 4; } /* the 4-bit terminator is the low half of d[2 + len], already zero */ for (i = 3 + len, k = 0; i < DATA_CW; i++, k ^= 1) d[i] = k ? 0x11 : 0xEC; /* blocks, their error correction, interleaved */ { u8 *e = CW + DATA_CW, *o = OUT; const u8 *b = d; int start[BLOCKS]; for (i = 0; i < BLOCKS; i++) { int n = i < SHORT_BLOCKS ? SHORT_DATA : SHORT_DATA + 1; start[i] = b - d; ecc(b, n, e + (i << 5) - (i << 2) - (i << 1)); /* i * 26 */ b += n; } for (j = 0; j <= SHORT_DATA; j++) for (i = 0; i < BLOCKS; i++) if (j < SHORT_DATA || i >= SHORT_BLOCKS) *o++ = d[start[i] + j]; for (j = 0; j < ECC; j++) for (i = 0; i < BLOCKS; i++) *o++ = e[(i << 5) - (i << 2) - (i << 1) + j]; } /* function patterns */ for (i = 0; i < N; i++) for (j = 0; j < N; j++) row[i][j] = 0; for (i = 0; i < N; i++) { fn(6, i, !(i & 1)); fn(i, 6, !(i & 1)); } finder(3, 3); finder(N - 4, 3); finder(3, N - 4); for (i = 0; i < 4; i++) for (j = 0; j < 4; j++) if (!((i == 0 && j == 0) || (i == 0 && j == 3) || (i == 3 && j == 0))) align(centres[i], centres[j]); /* version information */ v = 20; for (i = 0; i < 12; i++) v = (v << 1) ^ ((v >> 11) & 1 ? 0x1F25 : 0); v = (u32)20 << 12 | (v & 0xFFF); for (i = 0, j = 0, k = 0; i < 18; i++) { int a = N - 11 + j, b = k; fn(a, b, v >> i & 1); fn(b, a, v >> i & 1); if (++j == 3) j = 0, k++; } /* format information: level M is 00 */ v = mask; for (i = 0; i < 10; i++) v = (v << 1) ^ ((v >> 9) & 1 ? 0x537 : 0); v = (((u32)mask << 10) | (v & 0x3FF)) ^ 0x5412; for (i = 0; i <= 5; i++) fn(8, i, v >> i & 1); fn(8, 7, v >> 6 & 1); fn(8, 8, v >> 7 & 1); fn(7, 8, v >> 8 & 1); for (i = 9; i < 15; i++) fn(14 - i, 8, v >> i & 1); for (i = 0; i < 8; i++) fn(N - 1 - i, 8, v >> i & 1); for (i = 8; i < 15; i++) fn(8, N - 15 + i, v >> i & 1); fn(8, N - 8, 1); /* the codewords, two columns at a time, up and down, from the bottom right */ bits = RAW << 3; at = 0; for (k = N - 1; k >= 1; k -= 2) { int up; if (k == 6) k = 5; up = !((k + 1) & 2); for (i = 0; i < N; i++) for (j = 0; j < 2; j++) { int x = k - j, y = up ? N - 1 - i : i; if (row[y][x] & 2) continue; if (at < bits) { row[y][x] = OUT[at >> 3] >> (7 - (at & 7)) & 1; at++; } if (masked(mask, x, y)) row[y][x] ^= 1; } } } #ifndef QR_HOST /* Tiles from VRAM $2000 (tile 256), 27 by 27, the code two pixels a module inside a light * border of four modules, on plane A at columns 6-32, rows 1-27. Light is colour 1 (white), * dark colour 0. Each tile row is four modules, so a word is two of them. */ #define TILE0 256 #define SIDE 27 static u16 pair(const u8 *r, int mx) { u16 w = 0; int k; for (k = 0; k < 2; k++, mx++) w = w << 8 | (mx < 0 || mx >= N || !r || !(r[mx] & 1) ? 0x11 : 0x00); return w; } void qr_show(const u8 *payload, int len, int mask) { static int ready; int tx, ty, y; if (!ready) tables(), ready = 1; build(payload, len, mask); VCTRL32 = 0x40000000UL | (u32)(TILE0 << 5 & 0x3FFF) << 16 | (TILE0 << 5) >> 14; for (ty = 0; ty < SIDE; ty++) for (tx = 0; tx < SIDE; tx++) { int mx = (tx << 2) - 4; for (y = 0; y < 8; y++) { int my = (((ty << 3) + y) >> 1) - 4; const u8 *r = my >= 0 && my < N ? row[my] : 0; VDATA = pair(r, mx); VDATA = pair(r, mx + 2); } } for (ty = 0; ty < SIDE; ty++) { u32 a = 0xC000 + ((ty + 1) << 7) + (6 << 1); VCTRL32 = 0x40000000UL | (a & 0x3FFF) << 16 | a >> 14; for (tx = 0; tx < SIDE; tx++) VDATA = TILE0 + (ty << 4) + (ty << 3) + (ty << 1) + ty + tx; /* ty * 27 */ } } #else /* For checking on a PC: the module matrix. */ const u8 *qr_matrix(const u8 *payload, int len, int mask) { tables(); build(payload, len, mask); return M; } #endif