OpenOCD can now scan out the hacked IDCODE.
[riscv-isa-sim.git] / riscv / remote_bitbang.h
1 #ifndef REMOTE_BITBANG_H
2 #define REMOTE_BITBANG_H
3
4 #include <stdint.h>
5
6 #define DEBUG
7 #ifdef DEBUG
8 # define D(x) x
9 #else
10 # define D(x)
11 #endif // DEBUG
12
13 class sim_t;
14
15 template <typename T>
16 class circular_buffer_t
17 {
18 public:
19 // The buffer can store capacity-1 data elements.
20 circular_buffer_t(unsigned int capacity) : data(new T[capacity]),
21 start(0), end(0), capacity(capacity) {}
22 circular_buffer_t() : start(0), end(0), capacity(0) {}
23 ~circular_buffer_t() { delete[] data; }
24
25 T *data;
26 unsigned int start; // Data start, inclusive.
27 unsigned int end; // Data end, exclusive.
28 unsigned int capacity; // Size of the buffer.
29 unsigned int size() const;
30 bool empty() const { return start == end; }
31 bool full() const { return ((end+1) % capacity) == start; }
32 T entry(unsigned index) { return data[(start + index) % capacity]; }
33
34 // Return size and address of the block of RAM where more data can be copied
35 // to be added to the buffer.
36 unsigned int contiguous_empty_size() const;
37 T *contiguous_empty() { return data + end; }
38 void data_added(unsigned int bytes);
39
40 unsigned int contiguous_data_size() const;
41 T *contiguous_data() { return data + start; }
42 // Tell the buffer that some bytes were consumed from the start of the
43 // buffer.
44 void consume(unsigned int bytes);
45
46 void reset();
47
48 T operator[](unsigned int i) const { return data[(start + i) % capacity]; }
49
50 void append(const T *src, unsigned int count);
51 void append(T value);
52 };
53
54 typedef enum {
55 TEST_LOGIC_RESET,
56 RUN_TEST_IDLE,
57 SELECT_DR_SCAN,
58 CAPTURE_DR,
59 SHIFT_DR,
60 EXIT1_DR,
61 PAUSE_DR,
62 EXIT2_DR,
63 UPDATE_DR,
64 SELECT_IR_SCAN,
65 CAPTURE_IR,
66 SHIFT_IR,
67 EXIT1_IR,
68 PAUSE_IR,
69 EXIT2_IR,
70 UPDATE_IR
71 } jtag_state_t;
72
73 class jtag_tap_t
74 {
75 public:
76 jtag_tap_t() :
77 state(TEST_LOGIC_RESET) {}
78
79 void reset() {
80 state = TEST_LOGIC_RESET;
81 }
82
83 void set_pins(bool tck, bool tms, bool tdi) {
84 if (!_tck && tck) {
85 // Positive clock edge.
86
87 D(fprintf(stderr, "Next state: %d\n", state));
88
89 state = next[state][_tms];
90
91 switch (state) {
92 case TEST_LOGIC_RESET:
93 ir = 1;
94 break;
95 case CAPTURE_DR:
96 dr = 0xdeadbeef;
97 dr_length = 32;
98 break;
99 case SHIFT_DR:
100 _tdo = dr & 1;
101 dr >>= 1;
102 dr |= (uint64_t) _tdi << (dr_length-1);
103 break;
104 case UPDATE_DR:
105 break;
106 case CAPTURE_IR:
107 break;
108 case SHIFT_IR:
109 _tdo = ir & 1;
110 ir >>= 1;
111 ir = ir | (_tdi << (ir_length-1));
112 break;
113 case UPDATE_IR:
114 break;
115 default:
116 break;
117 }
118 }
119 _tck = tck;
120 _tms = tms;
121 _tdi = tdi;
122
123 D(fprintf(stderr, "tck=%d tms=%d tdi=%d tdo=%d ir=0x%x dr=0x%lx\n",
124 _tck, _tms, _tdi, _tdo, ir, dr));
125 }
126
127 bool tdo() const { return _tdo; }
128
129 private:
130 bool _tck, _tms, _tdi, _tdo;
131 uint32_t ir;
132 unsigned ir_length;
133 uint64_t dr;
134 unsigned dr_length;
135
136 jtag_state_t state;
137 const jtag_state_t next[16][2] = {
138 /* TEST_LOGIC_RESET */ { RUN_TEST_IDLE, TEST_LOGIC_RESET },
139 /* RUN_TEST_IDLE */ { RUN_TEST_IDLE, SELECT_DR_SCAN },
140 /* SELECT_DR_SCAN */ { CAPTURE_DR, SELECT_IR_SCAN },
141 /* CAPTURE_DR */ { SHIFT_DR, EXIT1_DR },
142 /* SHIFT_DR */ { SHIFT_DR, EXIT1_DR },
143 /* EXIT1_DR */ { PAUSE_DR, UPDATE_DR },
144 /* PAUSE_DR */ { PAUSE_DR, EXIT2_DR },
145 /* EXIT2_DR */ { SHIFT_DR, UPDATE_DR },
146 /* UPDATE_DR */ { RUN_TEST_IDLE, SELECT_DR_SCAN },
147 /* SELECT_IR_SCAN */ { CAPTURE_IR, TEST_LOGIC_RESET },
148 /* CAPTURE_IR */ { SHIFT_IR, EXIT1_IR },
149 /* SHIFT_IR */ { SHIFT_IR, EXIT1_IR },
150 /* EXIT1_IR */ { PAUSE_IR, UPDATE_IR },
151 /* PAUSE_IR */ { PAUSE_IR, EXIT2_IR },
152 /* EXIT2_IR */ { SHIFT_IR, UPDATE_IR },
153 /* UPDATE_IR */ { RUN_TEST_IDLE, SELECT_DR_SCAN }
154 };
155 };
156
157 class remote_bitbang_t
158 {
159 public:
160 // Create a new server, listening for connections from localhost on the given
161 // port.
162 remote_bitbang_t(uint16_t port, sim_t *sim);
163
164 // Do a bit of work.
165 void tick();
166
167 private:
168 jtag_tap_t tap;
169
170 int socket_fd;
171 int client_fd;
172 circular_buffer_t<uint8_t> recv_buf;
173 circular_buffer_t<uint8_t> send_buf;
174
175 // Check for a client connecting, and accept if there is one.
176 void accept();
177 // Read as much into recv_buf as possible.
178 void read();
179 // Write as much of send_buf as possible.
180 void write();
181
182 // Process the input buffer.
183 void process_input();
184 };
185
186 #endif