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

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