1 #ifndef REMOTE_BITBANG_H
2 #define REMOTE_BITBANG_H
16 class circular_buffer_t
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
; }
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
]; }
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
);
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
44 void consume(unsigned int bytes
);
48 T
operator[](unsigned int i
) const { return data
[(start
+ i
) % capacity
]; }
50 void append(const T
*src
, unsigned int count
);
75 static const unsigned idcode_ir
= 1;
76 static const unsigned idcode_dr
= 0xdeadbeef;
77 static const unsigned dtmcontrol_ir
= 0x10;
81 state(TEST_LOGIC_RESET
) {}
84 state
= TEST_LOGIC_RESET
;
87 void set_pins(bool tck
, bool tms
, bool tdi
) {
89 // Positive clock edge.
94 dr
|= (uint64_t) _tdi
<< (dr_length
-1);
98 ir
|= _tdi
<< (ir_length
-1);
103 state
= next
[state
][_tms
];
105 case TEST_LOGIC_RESET
:
118 D(fprintf(stderr
, "Unsupported IR: 0x%x\n", ir
));
143 D(fprintf(stderr
, "state=%2d tck=%d tms=%d tdi=%d tdo=%d ir=0x%x dr=0x%lx\n",
144 state
, _tck
, _tms
, _tdi
, _tdo
, ir
, dr
));
147 bool tdo() const { return _tdo
; }
150 bool _tck
, _tms
, _tdi
, _tdo
;
152 const unsigned ir_length
= 5;
156 uint32_t dtmcontrol
= 1;
159 const jtag_state_t next
[16][2] = {
160 /* TEST_LOGIC_RESET */ { RUN_TEST_IDLE
, TEST_LOGIC_RESET
},
161 /* RUN_TEST_IDLE */ { RUN_TEST_IDLE
, SELECT_DR_SCAN
},
162 /* SELECT_DR_SCAN */ { CAPTURE_DR
, SELECT_IR_SCAN
},
163 /* CAPTURE_DR */ { SHIFT_DR
, EXIT1_DR
},
164 /* SHIFT_DR */ { SHIFT_DR
, EXIT1_DR
},
165 /* EXIT1_DR */ { PAUSE_DR
, UPDATE_DR
},
166 /* PAUSE_DR */ { PAUSE_DR
, EXIT2_DR
},
167 /* EXIT2_DR */ { SHIFT_DR
, UPDATE_DR
},
168 /* UPDATE_DR */ { RUN_TEST_IDLE
, SELECT_DR_SCAN
},
169 /* SELECT_IR_SCAN */ { CAPTURE_IR
, TEST_LOGIC_RESET
},
170 /* CAPTURE_IR */ { SHIFT_IR
, EXIT1_IR
},
171 /* SHIFT_IR */ { SHIFT_IR
, EXIT1_IR
},
172 /* EXIT1_IR */ { PAUSE_IR
, UPDATE_IR
},
173 /* PAUSE_IR */ { PAUSE_IR
, EXIT2_IR
},
174 /* EXIT2_IR */ { SHIFT_IR
, UPDATE_IR
},
175 /* UPDATE_IR */ { RUN_TEST_IDLE
, SELECT_DR_SCAN
}
179 class remote_bitbang_t
182 // Create a new server, listening for connections from localhost on the given
184 remote_bitbang_t(uint16_t port
, sim_t
*sim
);
194 circular_buffer_t
<uint8_t> recv_buf
;
195 circular_buffer_t
<uint8_t> send_buf
;
197 // Check for a client connecting, and accept if there is one.
199 // Read as much into recv_buf as possible.
201 // Write as much of send_buf as possible.
204 // Process the input buffer.
205 void process_input();