comparison sw/sniff-dec/command.c @ 45:b0524d1dc6ef

simtrace3-sniff-dec: implement command decoding
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 31 Aug 2023 09:32:48 +0000
parents 118a12e9483b
children 5268246520de
comparison
equal deleted inserted replaced
44:74330513121e 45:b0524d1dc6ef
12 extern char linebuf[]; 12 extern char linebuf[];
13 extern int lineno; 13 extern int lineno;
14 extern unsigned rx_byte; 14 extern unsigned rx_byte;
15 extern int state; 15 extern int state;
16 16
17 static char cmd_start_timestamp[18];
18 static int cmd_start_line;
19 static char data_start_timestamp[18];
20 static int data_start_line;
21 static char sw1_timestamp[18];
22 static int sw1_line;
23
24 static u_char cmd_hdr[5];
25 static unsigned hdr_byte_count;
26 static unsigned data_total, data_sofar, data_thistime;
27 static u_char data_buf[256], sw1;
28
17 void 29 void
18 start_cmd_header() 30 start_cmd_header()
19 { 31 {
20 printf("input line %d: command header, end of implementation so far\n", 32 strcpy(cmd_start_timestamp, linebuf);
21 lineno); 33 cmd_start_line = lineno;
22 exit(0); 34 cmd_hdr[0] = rx_byte;
35 hdr_byte_count = 1;
36 state = STATE_CMD_HDR;
23 } 37 }
38
39 static void
40 print_cmd_hdr()
41 {
42 unsigned n;
43
44 printf("%s line %d: CMD", cmd_start_timestamp, cmd_start_line);
45 for (n = 0; n < 5; n++)
46 printf(" %02X", cmd_hdr[n]);
47 putchar('\n');
48 }
49
50 void
51 cmd_hdr_byte_in()
52 {
53 cmd_hdr[hdr_byte_count++] = rx_byte;
54 if (hdr_byte_count < 5)
55 return;
56 print_cmd_hdr();
57 if ((cmd_hdr[1] & 0xF0) == 0x60 || (cmd_hdr[1] & 0xF0) == 0x90) {
58 printf(" ERROR: INS byte is invalid!\n");
59 state = STATE_ERROR;
60 return;
61 }
62 if (cmd_hdr[4])
63 data_total = cmd_hdr[4];
64 else
65 data_total = 256;
66 data_sofar = 0;
67 state = STATE_CMD_PROC;
68 }
69
70 static void
71 print_data()
72 {
73 unsigned n;
74
75 printf("%s line %d: DATA\n", data_start_timestamp, data_start_line);
76 for (n = 0; n < data_sofar; n++) {
77 printf(" %02X", data_buf[n]);
78 if ((n & 15) == 15 || n == data_sofar - 1)
79 putchar('\n');
80 }
81 }
82
83 static void
84 handle_sw1()
85 {
86 strcpy(sw1_timestamp, linebuf);
87 sw1_line = lineno;
88 sw1 = rx_byte;
89 if (data_sofar)
90 print_data();
91 state = STATE_CMD_SW;
92 }
93
94 void
95 handle_ack(single)
96 {
97 if (data_sofar >= data_total) {
98 printf("%s line %d: ERROR: ACK for more data than possible\n",
99 linebuf, lineno);
100 state = STATE_ERROR;
101 return;
102 }
103 if (!data_sofar) {
104 strcpy(data_start_timestamp, linebuf);
105 data_start_line = lineno;
106 }
107 if (single)
108 data_thistime = 1;
109 else
110 data_thistime = data_total - data_sofar;
111 state = STATE_CMD_DATA;
112 }
113
114 void
115 cmd_proc_byte_in()
116 {
117 if (rx_byte == 0x60)
118 return;
119 if ((rx_byte & 0xF0) == 0x60 || (rx_byte & 0xF0) == 0x90) {
120 handle_sw1();
121 return;
122 }
123 if (rx_byte == cmd_hdr[1]) {
124 handle_ack(0);
125 return;
126 }
127 if (rx_byte == (cmd_hdr[1] ^ 0xFF)) {
128 handle_ack(1);
129 return;
130 }
131 printf("%s line %d: ERROR: invalid procedure byte\n", linebuf, lineno);
132 state = STATE_ERROR;
133 }
134
135 void
136 cmd_data_byte_in()
137 {
138 data_buf[data_sofar++] = rx_byte;
139 data_thistime--;
140 if (data_thistime)
141 return;
142 state = STATE_CMD_PROC;
143 }
144
145 void
146 cmd_sw2_byte_in()
147 {
148 printf("%s line %d: SW %02X %02X\n", sw1_timestamp, sw1_line, sw1,
149 rx_byte);
150 state = STATE_READY_FOR_CMD;
151 }