annotate sw/sniff-dec/dispatch.c @ 43:6bef393a700e

sw/Makefile: add sniff-dec
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 31 Aug 2023 08:47:50 +0000
parents 118a12e9483b
children 74330513121e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
41
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * Dispatching received FPGA words based on the current state.
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 */
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 #include <sys/types.h>
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <stdio.h>
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <stdlib.h>
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include "state.h"
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 extern char linebuf[];
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 extern int lineno;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 extern unsigned fpga_word;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 extern u_char parity_table[256];
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 extern u_char inverse_coding_table[256];
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 int state;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 int inverse_conv;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 unsigned rx_byte;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 #define PREFIX "%s line %d: "
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 static void
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 handle_rst_change()
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 {
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 if (fpga_word & 0x4000) {
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 printf(PREFIX "RST high, card session begin\n",
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 linebuf, lineno);
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 state = STATE_ATR_TS;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 atr_begin();
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 } else {
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 printf(PREFIX "RST low, card session end\n", linebuf, lineno);
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 state = STATE_RSTLOW;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 }
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 }
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 static int
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 byte_prelim_checks()
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 {
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 if (!(fpga_word & 0x4000)) {
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 printf(PREFIX "ERROR: char Rx with RST low\n", linebuf, lineno);
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 return 1;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 }
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 if (fpga_word & 0x0200) {
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 printf(PREFIX "ERROR: start bit midpoint sampled high\n",
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 linebuf, lineno);
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 return 1;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 }
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 if (fpga_word & 0x0400) {
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 printf(PREFIX "ISO 7816-3 section 7.3 error signal\n",
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 linebuf, lineno);
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 return 1;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 }
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 return 0;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 }
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 static int
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 check_parity()
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 {
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 unsigned expect_par;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 expect_par = (parity_table[fpga_word & 0xFF] ^ inverse_conv) << 8;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 if ((fpga_word & 0x100) != expect_par) {
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 printf(PREFIX "ERROR: bad character parity\n", linebuf, lineno);
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 state = STATE_ERROR;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 return 1;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 }
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 return 0;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 }
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 static void
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 extract_rx_byte()
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 {
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 if (inverse_conv)
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 rx_byte = inverse_coding_table[fpga_word & 0xFF];
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 else
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 rx_byte = fpga_word & 0xFF;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 }
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 void
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 process_fpga_word()
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 {
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 if (fpga_word & 0x8000) {
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 handle_rst_change();
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 return;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 }
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 if (state == STATE_ERROR)
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 return;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 if (byte_prelim_checks()) {
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 state = STATE_ERROR;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 return;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 }
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 switch (state) {
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 case STATE_UNDEF:
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 printf(PREFIX "ERROR: char Rx without preceding RST high\n",
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 linebuf, lineno);
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 state = STATE_ERROR;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 return;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 case STATE_RSTLOW:
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 printf(PREFIX "ERROR: char Rx after RST low\n",
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 linebuf, lineno);
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 state = STATE_ERROR;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 return;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 case STATE_ATR_TS:
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 if ((fpga_word & 0x1FF) == 0x13B) {
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 printf(PREFIX "TS sets direct coding convention\n",
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 linebuf, lineno);
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 inverse_conv = 0;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 rx_byte = 0x3B;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110 atr_byte_in();
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
111 state = STATE_ATR_CONT;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112 } else if ((fpga_word & 0x1FF) == 0x103) {
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113 printf(PREFIX "TS sets inverse coding convention\n",
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114 linebuf, lineno);
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 inverse_conv = 1;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116 rx_byte = 0x3F;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
117 atr_byte_in();
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
118 state = STATE_ATR_CONT;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
119 } else {
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
120 printf(PREFIX "ERROR: invalid char after RST high\n",
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
121 linebuf, lineno);
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
122 state = STATE_ERROR;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
123 }
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
124 return;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
125 case STATE_ATR_CONT:
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
126 if (check_parity())
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
127 return;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
128 extract_rx_byte();
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
129 atr_byte_in();
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
130 return;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
131 case STATE_READY_FOR_CMD:
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
132 if (check_parity())
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
133 return;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
134 extract_rx_byte();
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
135 if (rx_byte == 0xFF)
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
136 start_pps_msg();
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
137 else
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
138 start_cmd_header();
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
139 return;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
140 case STATE_PPS_MSG:
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
141 if (check_parity())
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
142 return;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
143 extract_rx_byte();
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
144 pps_byte_in();
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
145 return;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
146 default:
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
147 fprintf(stderr, "BUG in top state machine: invalid state\n");
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
148 abort();
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
149 }
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
150 }