#include "vend.h"
+#define COINMECH_ID 0x20
+
+u8 last_byte;
+
+
+u8 parity_test(u8 c) {
+ u8 parity = 0;
+ for (parity = 0; c; c = c>>1) {
+ if (c&1) parity = !parity;
+ }
+ return parity;
+}
+
+/*
+ * parity_good truth table:
+ *
+ * | parity_test(c)
+ * | 0 1
+ * -----+---------
+ * R8 0 | 1 0
+ * 1 | 0 1
+ */
+
+bool parity_good(u8 c) {
+ u8 R8 = (_io_ports[M6811_SCCR1] & M6811_R8)?1:0;
+ u8 p = parity_test(c)?1:0;
+ return R8 == p;
+}
+
+void send_byte(u8 c) {
+ last_byte = c;
+ while (!(_io_ports[M6811_SCSR] & M6811_TDRE)); /* wait for TD register empty */
+
+ if (parity_of(c))
+ bset(&_io_ports[M6811_SCCR1], M6811_T8);
+ else
+ bclr(&_io_ports[M6811_SCCR1], M6811_T8);
+}
+
+void ask_for_retrans() {
+ /* sends an 0xff down the line */
+ send_byte(0xff);
+}
+
+
+u8 packet_pos = 0;
+
+#define IS_CTRL(x) (x & 0x10) /* true if this packet is a control packet */
+void sci_interrupt() {
+ u8 in;
+ in = _io_ports[M6811_SCDR];
+
+ /* test for framing errors & parity bit */
+ if (_io_ports[M6811_SCSR] & M6811_FE || !parity_good(in)) {
+ _io_ports[M6811_SCDR]; /* read of register req'd to clear FE */
+ ask_for_retrans();
+ return;
+ }
+
+ /* all bytes must have the correct ID in the 3 MSBs */
+ if (in & 0xe0 != COINMECH_ID) return;
+
+ /* we have a good packet */
+ if (in == 0x3f) {
+ /* retransmit was requested */
+ send_byte(last_byte);
+ return;
+ }
+
+ if (packet_pos != 0 && !IS_CTRL(x&0x10)) {
+ switch (in & 0x0f) {
+ }
+ } else {
+
+ }
+}