4701adaa425b7cb68d65c691558ccaa31a043597
[uccvend-snackrom.git] / ROM2 / coinmech.c
1 #include "vend.h"
2
3 #define COINMECH_ID   0x20
4
5 u8 last_byte;
6
7 u8 parity_test(u8 c) {
8         u8 parity = 0;
9         for (parity = 0; c; c = c>>1) {
10                 if (c&1) parity = !parity;
11         }
12         return parity;
13 }
14
15 /*
16  * parity_good truth table:
17  *
18  *      | parity_test(c)
19  *      | 0   1
20  * -----+---------
21  * R8 0 | 1   0
22  *    1 | 0   1
23  */
24
25 bool parity_good(u8 c) {
26         u8 R8 = (_io_ports[M6811_SCCR1] & M6811_R8)?1:0;
27         u8 p = parity_test(c)?1:0;
28         return R8 == p;
29 }
30
31 void send_byte(u8 c) {
32         last_byte = c;
33         while (!(_io_ports[M6811_SCSR] & M6811_TDRE)); /* wait for TD register empty */
34
35         if (parity_test(c))
36                 bset((void*)&_io_ports[M6811_SCCR1], M6811_T8);
37         else
38                 bclr((void*)&_io_ports[M6811_SCCR1], M6811_T8);
39 }
40
41 void ask_for_retrans() {
42         /* sends an 0xff down the line */
43         send_byte(0xff);
44 }
45
46
47 u8 packet_pos = 0;
48
49 #define IS_CTRL(x) (x & 0x10) /* true if this packet is a control packet */
50 void sci_interrupt() {
51         u8 in;
52         in = _io_ports[M6811_SCDR];
53         
54         /* test for framing errors & parity bit */
55         if (_io_ports[M6811_SCSR] & M6811_FE || !parity_good(in)) {
56                 _io_ports[M6811_SCDR]; /* read of register req'd to clear FE */
57                 ask_for_retrans();
58                 return;
59         }
60
61         /* all bytes must have the correct ID in the 3 MSBs */
62         if ((in & 0xe0) != COINMECH_ID) return;
63
64         /* we have a good packet */
65         if (in == 0x3f) {
66                 /* retransmit was requested */
67                 send_byte(last_byte);
68                 return;
69         }
70
71         if (packet_pos != 0 && !IS_CTRL(in&0x10)) {
72                 switch (in & 0x0f) {
73                 }
74         } else {
75
76         }
77 }
78
79 void coin_eat() {
80 }

UCC git Repository :: git.ucc.asn.au