OpenCores
URL https://opencores.org/ocsvn/rs232_syscon/rs232_syscon/trunk

Subversion Repositories rs232_syscon

[/] [rs232_syscon/] [trunk/] [verilog/] [rs232_syscon.v] - Blame information for rev 9

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 8 jclaytons
//-------------------------------------------------------------------------------------
2
//
3
// Author: John Clayton
4
// Date  : June 25, 2001
5
// Update: 6/25/01 copied this file from ps2_mouse.v (pared down).
6
// Update: 6/07/01 Finished initial coding efforts.
7
// Update: 7/19/01 First compilation.  Added master_br_o and master_bg_i;
8
// Update: 7/25/01 Testing.  Eliminated msg_active signal.  Changed serial.v
9
//                 to reflect new handshaking (i.e. "load_request" is now a
10
//                 periodic pulse of tx_clk_1x from rs232_tx...)
11
// Update: 7/30/01 Began coding m2 state machine.  Defined response codes.
12
// Update: 8/01/01 After some testing with m2, merged m2 into m1.  Eliminated
13
//                 response codes.
14
// Update: 8/02/01 Tested & measured the single "combined" state machine's
15
//                 performance, and "it was found wanting."  (The 49.152MHz
16
//                 clock frequency was too fast for it...)  Created clk_s
17
//                 at 49.152/2 MHz, and this worked.
18
// Update: 8/03/01 Added counter loop to "execute" and "bus_granted" states
19
//                 so that multiple bus cycles are generated, at sequential
20
//                 addresses.  However, the qty field is not cleared before
21
//                 being loaded with new characters, which causes problems.
22
// Update: 8/07/01 Finished debugging.  The read print formatting is now
23
//                 correct, and the unit appears to operate correctly.
24
//                 Many hours were spent puzzling over how to make this work.
25
//                 Removed port "debug".
26
// Update: 8/24/01 Added "master_stb_i" and "master_we_i" inputs and logic.
27
// Update: 12/13/01 For memory_sizer.v, I lowered the frequency of clk_s down
28
//                 to 49.152/4 MHz, so I changed the CLOCK_FACTOR from 8 to 4
29
//                 on the rs232 transciever, and this worked fine.
30
// Update: 9/09/02 Incorporated the "autobaud_with_tracking" module so that
31
//                 the serial clock is generated automatically, no matter
32
//                 what frequency clk_i is used.  The user simply needs to
33
//                 press "enter" from the terminal program to synchronize
34
//                 the baud rate generator.  Changing BAUD rates on the fly
35
//                 is also permitted, simply change to a new BAUD rate in the
36
//                 terminal program and hit enter.
37
// Update:11/26/02 Changed the string constants to binary representation
38
//                 (Just to eliminate warnings in XST.)
39
//
40
//
41
//
42
//
43
//
44
// Description
45
//-------------------------------------------------------------------------------------
46
// This is a state-machine driven rs232 serial port interface to a "Wishbone"
47
// type of bus.  It is intended to be used as a "Wishbone system controller"
48
// for debugging purposes.  Specifically, the unit allows the user to send
49
// text commands to the "rs232_syscon" unit, in order to generate read and
50
// write cycles on the Wishbone compatible bus.  The command structure is
51
// quite terse and spartan in nature, this is for the sake of the logic itself.
52
// Because the menu-driven command structure is supported without the use of
53
// dedicated memory blocks (in order to maintain cross-platform portability
54
// as much as possible) the menus and command responses were kept as small
55
// as possible.  In most cases, the responses from the unit to the user
56
// consist of a "newline" and one or two visible characters.  The command
57
// structure consists of the following commands and responses:
58
//
59
// Command Syntax              Purpose
60
// ---------------             ---------------------------------------
61
// w aaaa dddd xx              Write data "dddd" starting at address "aaaa"
62
//                             perform this "xx" times at sequential addresses.
63
//                             (The quantity field is optional, default is 1).
64
// r aaaa xx                   Read data starting from address "aaaa."
65
//                             Perform this "xx" times at sequential addresses.
66
//                             (The quantity field is optional, default is 1).
67
// i                           Send a reset pulse to the system. (initialize).
68
//
69
// Response from rs232_syscon  Meaning
70
// --------------------------  ---------------------------------------
71
// OK                          Command received and performed.  No errors.
72
// ?                           Command buffer full, without receiving "enter."
73
// C?                          Command not recognized.
74
// A?                          Address field syntax error.
75
// D?                          Data field syntax error.
76
// Q?                          Quantity field syntax error.
77
// !                           No "ack_i", or else "err_i" received from bus.
78
// B!                          No "bg_i" received from master.
79
//
80
// NOTES on the operation of this unit:
81
//
82
// - The unit generates a command prompt which is "-> ".
83
// - Capitalization is not important.
84
// - Each command is terminated by the "enter" key (0x0d character).
85
//   Commands are executed as soon as "enter" is received.
86
// - Trailing parameters need not be re-entered.  Their values will
87
//   remain the same as their previous settings.
88
// - Use of the backspace key is supported, so mistakes can be corrected.
89
// - The length of the command line is limited to a fixed number of
90
//   characters, as configured by parameter.
91
// - Fields are separated by white space, including "tab" and/or "space"
92
// - All numerical fields are interpreted as hexadecimal numbers.
93
//   Decimal is not supported.
94
// - Numerical field values are retained between commands.  If a "r" is issued
95
//   without any fields following it, the previous values will be used.  A
96
//   set of "quantity" reads will take place at sequential addresses.
97
//   If a "w" is issued without any fields following it, the previous data
98
//   value will be written "quantity" times at sequential addresses, starting
99
//   from the next location beyond where the last command ended.
100
// - If the user does not wish to use "ack" functionality, simply tie the
101
//   "ack_i" input to 1b'1, and then the ! response will never be generated.
102
// - The data which is read in by the "r" command is displayed using lines
103
//   which begin with the address, followed by the data fields.  The number
104
//   of data fields displayed per line (following the address) is adjustable
105
//   by setting a parameter.  No other display format adjustments can be made.
106
// - There is currently only a single watchdog timer.  It begins to count at
107
//   the time a user hits "enter" to execute a command.  If the bus is granted
108
//   and the ack is received before the expiration of the timer, then the
109
//   cycle will complete normally.  Therefore, the watchdog timeout value
110
//   needs to include time for the request and granting of the bus, in
111
//   addition to the time needed for the actual bus cycle to complete.
112
//
113
//
114
// Currently, there is only a single indicator (stb_o) generated during bus
115
// output cycles which are generated from this unit.
116
// The user can easily implement decoding logic based upon adr_o and stb_o
117
// which would serve as multiple "stb_o" type signals for different cores
118
// which would be sharing the same bus.
119
//
120
// The dat_io bus supported by this module is a tri-state type of bus.  The
121
// Wishbone spec. allows for this type of bus (see Wishbone spec. pg. 66).
122
// However, if separate dat_o and dat_i busses are desired, they can be added
123
// to the module without too much trouble.  Supposedly the only difference
124
// between the two forms of data bus is that one of them avoids using tri-state
125
// at the cost of doubling the number of interconnects used to carry data back
126
// and forth...  Some people say that tri-state should be avoided for use
127
// in internal busses in ASICs.  Maybe they are right.
128
// But in FPGAs tri-state seems to work pretty well, even for internal busses.
129
//
130
// Parameters are provided to configure the width of the different command
131
// fields.  To simplify the logic for binary to hexadecimal conversion, these
132
// parameters allow adjustment in units of 1 hex digit, not anything smaller.
133
// If your bus has 10 bits, for instance, simply set the address width to 3
134
// which produces 12 bits, and then just don't use the 2 msbs of address
135
// output.
136
//
137
// No support for the optional Wishbone "retry" (rty_i) input is provided at
138
// this time.
139
// No support for "tagn_o" bits is provided at this time, although a register
140
// might be added external to this module in order to implement to tag bits.
141
// No BLOCK or RMW cycles are supported currently, so cyc_o is equivalent to
142
// stb_o...
143
// The output busses are not tri-stated.  The user may add tri-state buffers
144
// external to the module, using "stb_o" to enable the buffer outputs.
145
//
146
//-------------------------------------------------------------------------------------
147
 
148
 
149
`define NIBBLE_SIZE 4        // Number of bits in one nibble
150
 
151
// The command register has these values
152
`define CMD_0 0              // Unused command
153
`define CMD_I 1              // Initialize (or reset)
154
`define CMD_R 2              // Read
155
`define CMD_W 3              // Write
156
 
157
module rs232_syscon (
158
  clk_i,
159
  reset_i,
160
  ack_i,
161
  err_i,
162
  master_bg_i,
163
  master_adr_i,
164
  master_stb_i,
165
  master_we_i,
166
  rs232_rxd_i,
167
  dat_io,
168
  rst_o,
169
  master_br_o,
170
  stb_o,
171
  cyc_o,
172
  adr_o,
173
  we_o,
174
  rs232_txd_o
175
  );
176
 
177
 
178
// Parameters
179
 
180
// The timer value can be from [0 to (2^WATCHDOG_TIMER_BITS_PP)-1] inclusive.
181
// RD_FIELDS_PP can be from [0 to (2^RD_FIELD_CTR_BITS_PP)-1] inclusive.
182
// Ensure that (2^CHAR_COUNT_BITS_PP) >= CMD_BUFFER_SIZE_PP.
183
// The setting of CMD_BUFFER_SIZE_PP should be large enough to hold the
184
// largest command, obviously.
185
// Ensure that (2^RD_DIGIT_COUNT_BITS_PP) is greater than or equal to the
186
//     larger of {ADR_DIGITS_PP,DAT_DIGITS_PP}.
187
parameter ADR_DIGITS_PP = 4;             // # of hex digits for address.
188
parameter DAT_DIGITS_PP = 4;             // # of hex digits for data.
189
parameter QTY_DIGITS_PP = 2;             // # of hex digits for quantity.
190
parameter CMD_BUFFER_SIZE_PP = 32;       // # of chars in the command buffer.
191
parameter CMD_PTR_BITS_PP = 4;           // # of Bits in command buffer ptr.
192
parameter WATCHDOG_TIMER_VALUE_PP = 200; // # of sys_clks before ack expected.
193
parameter WATCHDOG_TIMER_BITS_PP  = 8;   // # of bits needed for timer.
194
parameter RD_FIELDS_PP = 8;              // # of fields/line (when qty > 1).
195
parameter RD_FIELD_COUNT_BITS_PP = 3;    // # of bits in the fields counter.
196
parameter RD_DIGIT_COUNT_BITS_PP = 2;    // # of bits in the digits counter.
197
 
198
 
199
// State encodings, provided as parameters
200
// for flexibility to the one instantiating the module.
201
// In general, the default values need not be changed.
202
 
203
// There is one state machines: m1.
204
// "default" state upon power-up and configuration is:
205
//    "m1_initial_state"
206
 
207
parameter m1_initial_state = 5'h00;
208
parameter m1_send_ok = 5'h01;                    // Sends OK
209
parameter m1_send_prompt = 5'h02;                // Sends "-> "
210
parameter m1_check_received_char = 5'h03;
211
parameter m1_send_crlf = 5'h04;                  // Sends cr,lf
212
parameter m1_parse_error_indicator_crlf = 5'h05; // Sends cr,lf
213
parameter m1_parse_error_indicator = 5'h06;      // Sends ?
214
parameter m1_ack_error_indicator = 5'h07;        // Sends !
215
parameter m1_bg_error_indicator = 5'h08;         // Sends B!
216
parameter m1_cmd_error_indicator = 5'h09;        // Sends C?
217
parameter m1_adr_error_indicator = 5'h0a;        // Sends A?
218
parameter m1_dat_error_indicator = 5'h0b;        // Sends D?
219
parameter m1_qty_error_indicator = 5'h0c;        // Sends Q?
220
parameter m1_scan_command = 5'h10;
221
parameter m1_scan_adr_whitespace = 5'h11;
222
parameter m1_get_adr_field = 5'h12;
223
parameter m1_scan_dat_whitespace = 5'h13;
224
parameter m1_get_dat_field = 5'h14;
225
parameter m1_scan_qty_whitespace = 5'h15;
226
parameter m1_get_qty_field = 5'h16;
227
parameter m1_start_execution = 5'h17;
228
parameter m1_request_bus = 5'h18;
229
parameter m1_bus_granted = 5'h19;
230
parameter m1_execute = 5'h1a;
231
parameter m1_rd_send_adr_sr = 5'h1b;
232
parameter m1_rd_send_separator = 5'h1c;
233
parameter m1_rd_send_dat_sr = 5'h1d;
234
parameter m1_rd_send_space = 5'h1e;
235
parameter m1_rd_send_crlf = 5'h1f;
236
 
237
// I/O declarations
238
input clk_i;                 // System clock input
239
input reset_i;               // Reset signal for this module
240
input ack_i;                 // Ack input from Wishbone "slaves"
241
input err_i;                 // Err input from Wishbone "slaves"
242
input master_bg_i;           // Bus Grant (grants this module the bus)
243
                             // Address bus input from "normal" Wishbone
244
                             // master (i.e. from processor)
245
input [`NIBBLE_SIZE*ADR_DIGITS_PP-1:0] master_adr_i;
246
input master_stb_i;          // bus cycle signal from "normal" bus master
247
input master_we_i;           // write enable from "normal" bus master
248
input rs232_rxd_i;           // Serial data from debug host terminal.
249
                             // Data bus (tri-state, to save interconnect)
250
inout [`NIBBLE_SIZE*DAT_DIGITS_PP-1:0] dat_io;
251
 
252
output rst_o;                // Rst output to Wishbone "slaves"
253
output master_br_o;          // Bus request to normal master device.
254
output stb_o;                // Bus cycle indicator to Wishbone "slaves"
255
output cyc_o;                // Bus cycle indicator to Wishbone "slaves"
256
                             // Address bus output to Wishbone "slaves"
257
output [`NIBBLE_SIZE*ADR_DIGITS_PP-1:0] adr_o;
258
output we_o;                 // Write enable to Wishbone "slaves"
259
output rs232_txd_o;          // Serial transmit data to debug host terminal
260
 
261
reg rst_o;
262
reg master_br_o;
263
 
264
// Internal signal declarations
265
wire watchdog_timer_done;   // High when watchdog timer is expired
266
wire rd_addr_field_done;    // High when displayed addr field is complete
267
wire rd_data_field_done;    // High when displayed data field is complete
268
wire rd_line_done;          // High when displayed line is complete
269
wire char_is_enter;         // High when cmd_buffer[char_count] is enter.
270
wire char_is_whitespace;    // High when cmd_buffer[char_count] is whitespace.
271
wire char_is_num;           // High when cmd_buffer[char_count] is 0..9
272
wire char_is_a_f;           // High when cmd_buffer[char_count] is a..f
273
wire char_is_hex;           // High when cmd_buffer[char_count] is a hex char.
274
wire char_is_r;             // High when cmd_buffer[char_count] is r.
275
wire char_is_w;             // High when cmd_buffer[char_count] is w.
276
wire char_is_i;             // High when cmd_buffer[char_count] is i.
277
wire rx_char_is_enter;      // High when rs232_rx_char is enter.
278
wire rx_char_is_backspace;  // High when rs232_rx_char is backspace.
279
wire [4:0] msg_pointer;     // Determines message position or address.
280
wire [3:0] hex_digit;       // This is the digit to be stored.
281
 
282
reg rs232_echo;           // High == echo char's received.
283
reg [7:0] msg_char;       // Selected response message character.
284
reg [4:0] msg_base;       // Added to msg_offset to form msg_pointer.
285
reg [4:0] msg_offset;     // Offset from start of message.
286
reg reset_msg_offset;     // High == set message offset to zero
287
reg incr_msg_offset;      // Used for output messages.
288
reg cmd_i;                // Sets command.
289
reg cmd_r;                // Sets command.
290
reg cmd_w;                // Sets command.
291
reg shift_rd_adr;         // Shifts the rd_adr_sr by one character.
292
reg store_adr;            // Allows adr_sr to store hex_digit.
293
reg store_dat;            // Allows dat_sr to store hex_digit.
294
reg store_qty;            // Allows qty_sr to store hex_digit.
295
reg reset_adr;            // Clears adr_sr
296
reg reset_dat;            // Clears dat_sr
297
reg reset_qty;            // Clears qty_sr
298
reg init_qty;             // Sets qty_sr to 1
299
reg capture_dat;          // Puts dat_io into dat_sr for later display.
300
 
301
    // For the buses
302
wire [`NIBBLE_SIZE*ADR_DIGITS_PP-1:0] adr_ptr;  // = adr_sr + adr_offset
303
 
304
reg stb_l;      // "local" stb signal (to distinguish from stb_o)
305
reg we_l;       // "local" we  signal (to distinguish from we_o)
306
 
307
reg [`NIBBLE_SIZE*ADR_DIGITS_PP-1:0] rd_adr_sr; // sr for printing addresses
308
reg [`NIBBLE_SIZE*ADR_DIGITS_PP-1:0] adr_sr;    // "nibble" shift register
309
reg [`NIBBLE_SIZE*DAT_DIGITS_PP-1:0] dat_sr;    // "nibble" shift register
310
reg [`NIBBLE_SIZE*QTY_DIGITS_PP-1:0] qty_sr;    // "nibble" shift register
311
reg [1:0] command;
312
reg [`NIBBLE_SIZE*QTY_DIGITS_PP-1:0] adr_offset;   // counts from 0 to qty_sr
313
reg reset_adr_offset;
314
reg incr_adr_offset;
315
 
316
    // For the command buffer
317
reg [CMD_PTR_BITS_PP-1:0] cmd_ptr; // Offset from start of command.
318
reg reset_cmd_ptr;        // High == set command pointer to zero.
319
reg incr_cmd_ptr;         // Used for "write port" side of the command buffer
320
reg decr_cmd_ptr;         // Used for "write port" side of the command buffer
321
reg cmd_buffer_write;
322
reg [7:0] cmd_buffer [0:CMD_BUFFER_SIZE_PP-1];
323
wire [7:0] cmd_char;
324
wire [7:0] lc_cmd_char;   // Lowercase version of cmd_char
325
 
326
    // For the state machine
327
reg [4:0] m1_state;
328
reg [4:0] m1_next_state;
329
 
330
    // For various counters
331
reg reset_rd_field_count;
332
reg reset_rd_digit_count;
333
reg incr_rd_field_count;
334
reg incr_rd_digit_count;
335
reg [RD_FIELD_COUNT_BITS_PP-1:0] rd_field_count;  // "fields displayed"
336
reg [RD_DIGIT_COUNT_BITS_PP-1:0] rd_digit_count;  // "digits displayed"
337
reg [WATCHDOG_TIMER_BITS_PP-1:0] watchdog_timer_count;
338
reg reset_watchdog;
339
 
340
     // For the rs232 interface
341
wire serial_clk;
342
wire [2:0] rs232_rx_error;
343
wire rs232_tx_load;
344
wire rs232_tx_load_request;
345
wire rs232_rx_data_ready;
346
wire [7:0] rs232_rx_char;
347
wire [7:0] rs232_tx_char;   // Either rs232_rx_char or msg_char
348
 
349
//--------------------------------------------------------------------------
350
// Instantiations
351
//--------------------------------------------------------------------------
352
 
353
 
354
// These defines are for the rs232 interface
355
`define START_BITS 1
356
`define DATA_BITS 8
357
`define STOP_BITS 1
358
`define CLOCK_FACTOR 8
359
 
360
// This module generates a serial BAUD clock automatically.
361
// The unit synchronizes on the carriage return character, so the user
362
// only needs to press the "enter" key for serial communications to start
363
// working, no matter what BAUD rate and clk_i frequency are used!
364
auto_baud_with_tracking #(
365
                          `CLOCK_FACTOR,    // CLOCK_FACTOR_PP
366
                          16                // LOG2_MAX_COUNT_PP
367
                          )
368
  clock_unit_2
369
  (
370
  .clk_i(clk_i),
371
  .reset_i(reset_i),
372
  .serial_dat_i(rs232_rxd_i),
373
  .auto_baud_locked_o(),
374
  .baud_clk_o(serial_clk)
375
  );
376
 
377
// A transmitter, which asserts load_request at the end of the currently
378
// transmitted word.  The tx_clk must be a "clock enable" (narrow positive
379
// pulse) which occurs at 16x the desired transmit rate.  If load_request
380
// is connected directly to load, the unit will transmit continuously.
381
rs232tx #(
382
          `START_BITS,   // start_bits
383
          `DATA_BITS,    // data_bits
384
          `STOP_BITS,    // stop_bits (add intercharacter delay...)
385
          `CLOCK_FACTOR  // clock_factor
386
         )
387
         rs232_tx_block // instance name
388
         (
389
          .clk(clk_i),
390
          .tx_clk(serial_clk),
391
          .reset(reset_i),
392
          .load(rs232_tx_load),
393
          .data(rs232_tx_char),
394
          .load_request(rs232_tx_load_request),
395
          .txd(rs232_txd_o)
396
         );
397
 
398
// A receiver, which asserts "word_ready" to indicate a received word.
399
// Asserting "read_word" will cause "word_ready" to go low again if it was high.
400
// The character is held in the output register, during the time the next
401
//   character is coming in.
402
rs232rx #(
403
          `START_BITS,  // start_bits
404
          `DATA_BITS,   // data_bits
405
          `STOP_BITS,   // stop_bits
406
          `CLOCK_FACTOR // clock_factor
407
         )
408
         rs232_rx_block // instance name
409
         (
410
          .clk(clk_i),
411
          .rx_clk(serial_clk),
412
          .reset(reset_i || (| rs232_rx_error)),
413
          .rxd(rs232_rxd_i),
414
          .read(rs232_tx_load),
415
          .data(rs232_rx_char),
416
          .data_ready(rs232_rx_data_ready),
417
          .error_over_run(rs232_rx_error[0]),
418
          .error_under_run(rs232_rx_error[1]),
419
          .error_all_low(rs232_rx_error[2])
420
         );
421
 
422
//`undef START_BITS 
423
//`undef DATA_BITS 
424
//`undef STOP_BITS 
425
//`undef CLOCK_FACTOR
426
 
427
 
428
//--------------------------------------------------------------------------
429
// Module code
430
//--------------------------------------------------------------------------
431
 
432
assign adr_o = master_bg_i?adr_ptr:master_adr_i;
433
assign we_o = master_bg_i?we_l:master_we_i;
434
assign stb_o = master_bg_i?stb_l:master_stb_i;
435
 
436
 
437
assign dat_io = (master_bg_i && we_l && stb_l)?
438
                   dat_sr:{`NIBBLE_SIZE*DAT_DIGITS_PP{1'bZ}};
439
 
440
// Temporary
441
assign cyc_o = stb_o;  // Separate cyc_o is not yet supported!
442
 
443
 
444
// This is the adress offset counter
445
always @(posedge clk_i)
446
begin
447
  if (reset_i || reset_adr_offset) adr_offset <= 0;
448
  else if (incr_adr_offset) adr_offset <= adr_offset + 1;
449
end
450
// This forms the adress pointer which is used on the bus.
451
assign adr_ptr = adr_sr + adr_offset;
452
 
453
 
454
// This is the ROM for the ASCII characters to be transmitted.
455
always @(msg_pointer)
456
begin
457
  case (msg_pointer) // synthesis parallel_case
458
    5'b00000 : msg_char <= 8'h30;  //  "0"; // Hexadecimal characters
459
    5'b00001 : msg_char <= 8'h31;  //  "1";
460
    5'b00010 : msg_char <= 8'h32;  //  "2";
461
    5'b00011 : msg_char <= 8'h33;  //  "3";
462
    5'b00100 : msg_char <= 8'h34;  //  "4";
463
    5'b00101 : msg_char <= 8'h35;  //  "5";
464
    5'b00110 : msg_char <= 8'h36;  //  "6";
465
    5'b00111 : msg_char <= 8'h37;  //  "7";
466
    5'b01000 : msg_char <= 8'h38;  //  "8";
467
    5'b01001 : msg_char <= 8'h39;  //  "9";
468
    5'b01010 : msg_char <= 8'h41;  //  "A"; // Address error indication
469
    5'b01011 : msg_char <= 8'h42;  //  "B";
470
    5'b01100 : msg_char <= 8'h43;  //  "C"; // Command error indication
471
    5'b01101 : msg_char <= 8'h44;  //  "D"; // Data error indication
472
    5'b01110 : msg_char <= 8'h45;  //  "E";
473
    5'b01111 : msg_char <= 8'h46;  //  "F";
474
    5'b10000 : msg_char <= 8'h20;  //  " "; // Space
475
    5'b10001 : msg_char <= 8'h3A;  //  ":"; // Colon
476
    5'b10010 : msg_char <= 8'h20;  //  " "; // Space
477
    5'b10011 : msg_char <= 8'h3F;  //  "?"; // Parse error indication
478
    5'b10100 : msg_char <= 8'h21;  //  "!"; // ack_i/err_i error indication
479
    5'b10101 : msg_char <= 8'h4F;  //  "O"; // "All is well" message
480
    5'b10110 : msg_char <= 8'h4B;  //  "K";
481
    5'b10111 : msg_char <= 8'h0D;  // Carriage return & line feed
482
    5'b11000 : msg_char <= 8'h0A;
483
    5'b11001 : msg_char <= 8'h2D;  //  "-"; // Command Prompt
484
    5'b11010 : msg_char <= 8'h3E;  //  ">";
485
    5'b11011 : msg_char <= 8'h20;  //  " ";
486
    5'b11100 : msg_char <= 8'h51;  //  "Q"; // Quantity error indication
487
    default  : msg_char <= 8'h3D;  //  "=";
488
  endcase
489
end
490
 
491
// This logic determines when to load a transmit character.
492
assign rs232_tx_load = rs232_echo?
493
  (rs232_rx_data_ready && rs232_tx_load_request):rs232_tx_load_request;
494
 
495
// This is the counter for incrementing, decrementing or resetting the 
496
// message pointer.
497
always @(posedge clk_i)
498
begin
499
  if (reset_i || reset_msg_offset) msg_offset <= 0;
500
  else if (incr_msg_offset) msg_offset <= msg_offset + 1;
501
end
502
assign msg_pointer = msg_offset + msg_base;
503
 
504
 
505
 
506
// This is the mux which selects whether to echo back the characters
507
// received (as during the entering of a command) or to send back response
508
// characters.
509
assign rs232_tx_char = (rs232_echo)?rs232_rx_char:msg_char;
510
 
511
 
512
// These assigments are for detecting whether the received rs232 character is
513
// anything of special interest.
514
assign rx_char_is_enter = (rs232_rx_char == 8'h0d);
515
assign rx_char_is_backspace = (rs232_rx_char == 8'h08);
516
 
517
 
518
 
519
// This is state machine m1.  It handles receiving the command line, including
520
// backspaces, and prints error/response messages.  It also parses and
521
// executes the commands.
522
 
523
// State register
524
always @(posedge clk_i)
525
begin : m1_state_register
526
  if (reset_i) m1_state <= m1_initial_state; // perform reset for rest of system
527
  else m1_state <= m1_next_state;
528
end
529
 
530
// State transition logic
531
always @(m1_state
532
         or rx_char_is_enter
533
         or rx_char_is_backspace
534
         or msg_offset
535
         or cmd_ptr
536
         or rs232_tx_load
537
         or char_is_whitespace
538
         or char_is_hex
539
         or char_is_enter
540
         or char_is_i
541
         or char_is_r
542
         or char_is_w
543
         or command
544
         or master_bg_i
545
         or watchdog_timer_done
546
         or err_i
547
         or ack_i
548
         or adr_offset
549
         or qty_sr
550
         or dat_sr
551
         or rd_adr_sr
552
         or rd_field_count
553
         or rd_digit_count
554
         )
555
begin : m1_state_logic
556
 
557
  // Default values for outputs.  The individual states can override these.
558
  msg_base <= 5'b0;
559
  reset_msg_offset <= 0;
560
  incr_msg_offset <= 0;
561
  rs232_echo <= 0;
562
  rst_o <= 0;
563
  we_l <= 0;
564
  stb_l <= 0;
565
  cmd_buffer_write <= 0;
566
  reset_cmd_ptr <= 0;
567
  incr_cmd_ptr <= 0;
568
  decr_cmd_ptr <= 0;
569
  master_br_o <= 0;
570
  cmd_r <= 0;
571
  cmd_w <= 0;
572
  cmd_i <= 0;
573
  shift_rd_adr <= 0;
574
  store_adr <= 0;          // enables storing hex chars in adr_sr (shift)
575
  store_dat <= 0;          // enables storing hex chars in dat_sr (shift)
576
  store_qty <= 0;          // enables storing hex chars in qty_sr (shift)
577
  reset_adr <= 0;
578
  reset_dat <= 0;
579
  reset_qty <= 0;
580
  init_qty <= 0;
581
  capture_dat <= 0;        // enables capturing bus data in dat_sr (load)
582
  incr_adr_offset <= 0;
583
  reset_adr_offset <= 0;
584
  reset_watchdog <= 0;
585
  incr_rd_field_count <= 0;
586
  incr_rd_digit_count <= 0;
587
  reset_rd_field_count <= 0;
588
  reset_rd_digit_count <= 0;
589
 
590
  case (m1_state) // synthesis parallel_case
591
 
592
    m1_initial_state :
593
      begin
594
        incr_msg_offset <= rs232_tx_load;
595
        if ((msg_offset == 15) && rs232_tx_load) begin
596
          m1_next_state <= m1_send_prompt;
597
          reset_msg_offset <= 1;
598
        end
599
        else m1_next_state <= m1_initial_state;
600
      end
601
 
602
    m1_send_ok :
603
      begin
604
        msg_base <= 5'b10101;     // Address of the OK message
605
        incr_msg_offset <= rs232_tx_load;
606
        if ((msg_offset == 1) && rs232_tx_load) begin
607
          m1_next_state <= m1_send_prompt;
608
          reset_msg_offset <= 1;
609
        end
610
        else m1_next_state <= m1_send_ok;
611
      end
612
 
613
    m1_send_prompt :
614
      begin
615
        msg_base <= 5'b10111;     // Address of the cr,lf,prompt message
616
        incr_msg_offset <= rs232_tx_load;
617
        if ((msg_offset == 4) && rs232_tx_load) begin
618
          m1_next_state <= m1_check_received_char;
619
          reset_cmd_ptr <= 1;
620
        end
621
        else m1_next_state <= m1_send_prompt;
622
      end
623
 
624
    // This state always leads to activating the parser...
625
    m1_send_crlf :
626
      begin
627
        msg_base <= 5'b10111;     // Address of the cr/lf message
628
        incr_msg_offset <= rs232_tx_load;
629
        if ((msg_offset == 1) && rs232_tx_load) begin
630
          m1_next_state <= m1_scan_command;
631
          reset_cmd_ptr <= 1;
632
        end
633
        else m1_next_state <= m1_send_crlf;
634
      end
635
 
636
    m1_check_received_char :
637
      begin
638
        rs232_echo <= 1;          // Allow echoing of characters
639
        if (rx_char_is_backspace && rs232_tx_load)
640
        begin
641
          m1_next_state <= m1_check_received_char;
642
          decr_cmd_ptr <= 1;     // This effectively eliminates the last char
643
        end
644
        else if (rx_char_is_enter && rs232_tx_load)
645
        begin
646
          m1_next_state <= m1_send_crlf;
647
          cmd_buffer_write <= 1;  // Store the enter as "marker" for parsing
648
          reset_msg_offset <= 1;
649
        end
650
        else if (rs232_tx_load && (cmd_ptr == CMD_BUFFER_SIZE_PP-1))
651
        begin
652
          m1_next_state <= m1_parse_error_indicator_crlf;
653
          reset_msg_offset <= 1;
654
          reset_cmd_ptr <= 1;
655
        end
656
        else if (rs232_tx_load)
657
        begin
658
          incr_cmd_ptr <= 1;
659
          cmd_buffer_write <= 1;
660
          m1_next_state <= m1_check_received_char;
661
        end
662
        else m1_next_state <= m1_check_received_char;
663
      end
664
 
665
    m1_bg_error_indicator :
666
      begin
667
        msg_base <= 5'b01011;    // Address of the B character
668
        incr_msg_offset <= rs232_tx_load;
669
        if ((msg_offset == 0) && rs232_tx_load) begin
670
          m1_next_state <= m1_ack_error_indicator;
671
          reset_msg_offset <= 1;
672
        end
673
        else m1_next_state <= m1_bg_error_indicator;
674
      end
675
 
676
    m1_ack_error_indicator :
677
      begin
678
        msg_base <= 5'b10100;    // Address of the ! error message
679
        incr_msg_offset <= rs232_tx_load;
680
        if ((msg_offset == 0) && rs232_tx_load) begin
681
          m1_next_state <= m1_send_prompt;
682
          reset_msg_offset <= 1;
683
        end
684
        else m1_next_state <= m1_ack_error_indicator;
685
      end
686
 
687
    // This state is used when the line is too long...
688
    m1_parse_error_indicator_crlf :
689
      begin
690
        msg_base <= 5'b10111;    // Address of the cr,lf message.
691
        incr_msg_offset <= rs232_tx_load;
692
        if ((msg_offset == 1) && rs232_tx_load) begin
693
          m1_next_state <= m1_parse_error_indicator;
694
          reset_msg_offset <= 1;
695
        end
696
        else m1_next_state <= m1_parse_error_indicator_crlf;
697
      end
698
 
699
    m1_parse_error_indicator :
700
      begin
701
        msg_base <= 5'b10011;    // Address of the ? message.
702
        incr_msg_offset <= rs232_tx_load;
703
        if ((msg_offset == 0) && rs232_tx_load) begin
704
          m1_next_state <= m1_send_prompt;
705
          reset_msg_offset <= 1;
706
        end
707
        else m1_next_state <= m1_parse_error_indicator;
708
      end
709
 
710
    m1_cmd_error_indicator :
711
      begin
712
        msg_base <= 5'b01100;    // Address of 'C'
713
        incr_msg_offset <= rs232_tx_load;
714
        if ((msg_offset == 0) && rs232_tx_load) begin
715
          m1_next_state <= m1_parse_error_indicator;
716
          reset_msg_offset <= 1;
717
        end
718
        else m1_next_state <= m1_cmd_error_indicator;
719
      end
720
 
721
    m1_adr_error_indicator :
722
      begin
723
        msg_base <= 5'b01010;    // Address of 'A'
724
        incr_msg_offset <= rs232_tx_load;
725
        if ((msg_offset == 0) && rs232_tx_load)
726
        begin
727
          m1_next_state <= m1_parse_error_indicator;
728
          reset_msg_offset <= 1;
729
        end
730
        else m1_next_state <= m1_adr_error_indicator;
731
      end
732
 
733
    m1_dat_error_indicator :
734
      begin
735
        msg_base <= 5'b01101;    // Address of 'D'
736
        incr_msg_offset <= rs232_tx_load;
737
        if ((msg_offset == 0) && rs232_tx_load)
738
        begin
739
          m1_next_state <= m1_parse_error_indicator;
740
          reset_msg_offset <= 1;
741
        end
742
        else m1_next_state <= m1_dat_error_indicator;
743
      end
744
 
745
    m1_qty_error_indicator :
746
      begin
747
        msg_base <= 5'b11100;    // Address of 'Q'
748
        incr_msg_offset <= rs232_tx_load;
749
        if ((msg_offset == 0) && rs232_tx_load)
750
        begin
751
          m1_next_state <= m1_parse_error_indicator;
752
          reset_msg_offset <= 1;
753
        end
754
        else m1_next_state <= m1_qty_error_indicator;
755
      end
756
 
757
    // The following states are for parsing and executing the command.
758
 
759
    // This state takes care of leading whitespace before the command
760
    m1_scan_command :
761
      begin
762
        rs232_echo <= 1;          // Don't send message characters
763
        reset_msg_offset <= 1;    // This one reset should cover all of the
764
                                  // parse/exec. states.  With rs232_echo
765
                                  // on, and no receive characters arrive,
766
                                  // then the msg_offset will remain reset.
767
                                  // This means the watchdog timer can take
768
                                  // a long time, if need be, during exec.
769
                                  // (NOTE: It might be better to disable
770
                                  //  the echoing of rx chars during these
771
                                  //  states.)
772
        init_qty <= 1;         // Set qty = 1 by default.  That can be
773
                               // overridden later, if the command has
774
                               // a different qty field.
775
        if (char_is_whitespace) begin
776
          m1_next_state <= m1_scan_command;
777
          incr_cmd_ptr <= 1;
778
        end
779
        else if (char_is_r) begin
780
          m1_next_state <= m1_scan_adr_whitespace;
781
          incr_cmd_ptr <= 1;
782
          cmd_r <= 1;
783
        end
784
        else if (char_is_w) begin
785
          m1_next_state <= m1_scan_adr_whitespace;
786
          incr_cmd_ptr <= 1;
787
          cmd_w <= 1;
788
        end
789
        else if (char_is_i) begin
790
          m1_next_state <= m1_start_execution;
791
          cmd_i <= 1;
792
        end
793
        else m1_next_state <= m1_cmd_error_indicator;
794
      end
795
 
796
    // The only way to determine the end of a valid field is to find
797
    // whitespace.  Therefore, char_is_whitespace must be used as an exit
798
    // condition from the "get_xxx_field" states.  So, this state is used to
799
    // scan through any leading whitespace prior to it.
800
    m1_scan_adr_whitespace :
801
      begin
802
        rs232_echo <= 1;          // Don't send message characters
803
        if (char_is_whitespace) begin
804
          m1_next_state <= m1_scan_adr_whitespace;
805
          incr_cmd_ptr <= 1;
806
        end
807
        else if (char_is_enter) m1_next_state <= m1_start_execution;
808
        else begin
809
          m1_next_state <= m1_get_adr_field;
810
          reset_adr <= 1;
811
        end
812
      end
813
 
814
    m1_get_adr_field :
815
      begin
816
        rs232_echo <= 1;          // Don't send message characters
817
        if (char_is_hex) begin
818
          m1_next_state <= m1_get_adr_field;
819
          store_adr <= 1;
820
          incr_cmd_ptr <= 1;
821
        end
822
        else if (char_is_whitespace) begin            // Normal exit
823
          m1_next_state <= m1_scan_dat_whitespace;
824
        end
825
        else if (char_is_enter) m1_next_state <= m1_start_execution;
826
        else m1_next_state <= m1_adr_error_indicator;
827
      end
828
 
829
    m1_scan_dat_whitespace :
830
      begin
831
        rs232_echo <= 1;          // Don't send message characters
832
        // There is no DAT field for reads, so skip it.
833
        if (command == `CMD_R) m1_next_state <= m1_scan_qty_whitespace;
834
        else if (char_is_whitespace) begin
835
          m1_next_state <= m1_scan_dat_whitespace;
836
          incr_cmd_ptr <= 1;
837
        end
838
        else if (char_is_enter) m1_next_state <= m1_start_execution;
839
        else begin
840
          m1_next_state <= m1_get_dat_field;
841
          reset_dat <= 1;
842
        end
843
      end
844
 
845
    m1_get_dat_field :
846
      begin
847
        rs232_echo <= 1;          // Don't send message characters
848
        if (char_is_hex) begin
849
          m1_next_state <= m1_get_dat_field;
850
          store_dat <= 1;
851
          incr_cmd_ptr <= 1;
852
        end
853
        else if (char_is_whitespace) begin            // Normal exit
854
          m1_next_state <= m1_scan_qty_whitespace;
855
        end
856
        else if (char_is_enter) m1_next_state <= m1_start_execution;
857
        else m1_next_state <= m1_dat_error_indicator;
858
      end
859
 
860
    m1_scan_qty_whitespace :
861
      begin
862
        rs232_echo <= 1;          // Don't send message characters
863
        if (char_is_whitespace) begin
864
          m1_next_state <= m1_scan_qty_whitespace;
865
          incr_cmd_ptr <= 1;
866
        end
867
        else if (char_is_enter) m1_next_state <= m1_start_execution;
868
        else begin
869
          m1_next_state <= m1_get_qty_field;
870
          reset_qty <= 1;
871
        end
872
      end
873
 
874
    m1_get_qty_field :
875
      begin
876
        rs232_echo <= 1;          // Don't send message characters
877
        if (char_is_hex) begin
878
          m1_next_state <= m1_get_qty_field;
879
          store_qty <= 1;
880
          incr_cmd_ptr <= 1;
881
        end
882
        else if (char_is_whitespace || char_is_enter) begin  // Normal exit
883
          m1_next_state <= m1_start_execution;
884
        end
885
        else m1_next_state <= m1_qty_error_indicator;
886
      end
887
 
888
    // This state seeks to obtain master_bg_i, which grants the bus to
889
    // rs232_syscon.
890
    m1_start_execution :
891
      begin
892
        rs232_echo <= 1;           // Don't send message characters
893
        reset_watchdog <= 1;       // Reset the timer.
894
        reset_adr_offset <= 1;     // Reset the address offset.
895
        reset_rd_field_count <= 1; // Reset the rd_field_count.
896
        m1_next_state <= m1_request_bus;
897
      end
898
 
899
    m1_request_bus :
900
      begin
901
        rs232_echo <= 1;          // Don't send message characters
902
        master_br_o <= 1;         // Request the bus.
903
        if (master_bg_i) m1_next_state <= m1_bus_granted;
904
        else if (watchdog_timer_done) begin
905
          m1_next_state <= m1_bg_error_indicator;
906
        end
907
        else m1_next_state <= m1_request_bus;
908
      end
909
 
910
    m1_bus_granted :
911
      begin
912
        rs232_echo <= 1;          // Don't send message characters
913
        master_br_o <= 1;         // Keep holding the bus
914
        reset_watchdog <= 1;      // Reset the timer.
915
        if (adr_offset != qty_sr) m1_next_state <= m1_execute;
916
        else m1_next_state <= m1_send_ok;
917
      end
918
 
919
    // This single state does reset/write/read depending upon the value
920
    // contained in "command"!
921
    m1_execute :
922
      begin
923
        rs232_echo <= 1;          // Don't send message characters
924
        master_br_o <= 1;         // Keep holding the bus
925
        stb_l <= 1'b1;            // Show that a bus cycle is happening
926
        case (command)            // Assert the appropriate signals
927
          `CMD_I : rst_o <= 1;
928
          `CMD_R : capture_dat <= ack_i;
929
          `CMD_W : we_l <= 1;
930
          default: ;
931
        endcase
932
        if (watchdog_timer_done || err_i) begin
933
          m1_next_state <= m1_ack_error_indicator;
934
        end
935
        else if (ack_i
936
                 && (command == `CMD_R)
937
                 && (rd_field_count == 0)
938
                 )
939
        begin
940
          m1_next_state <= m1_rd_send_adr_sr; // Leads to a new address line.
941
          reset_rd_digit_count <= 1;
942
          incr_adr_offset <= 1;               // move to the next address
943
        end
944
        else if (ack_i && (command == `CMD_R)) begin
945
          m1_next_state <= m1_rd_send_dat_sr; // Leads to a new data field.
946
          reset_rd_digit_count <= 1;
947
          reset_msg_offset <= 1;
948
          incr_adr_offset <= 1;             // move to the next address
949
        end
950
        else if (ack_i) begin
951
          m1_next_state <= m1_bus_granted;  // continue to the next cycle
952
          incr_adr_offset <= 1;             // move to the next address
953
        end
954
        else m1_next_state <= m1_execute;
955
      end
956
 
957
    m1_rd_send_adr_sr :
958
      begin
959
        msg_base <= {1'b0,rd_adr_sr[`NIBBLE_SIZE*ADR_DIGITS_PP-1:
960
                                    `NIBBLE_SIZE*(ADR_DIGITS_PP-1)]};
961
        if ((rd_digit_count == ADR_DIGITS_PP-1) && rs232_tx_load) begin
962
          m1_next_state <= m1_rd_send_separator;
963
          reset_msg_offset <= 1;
964
        end
965
        else if (rs232_tx_load) begin
966
          shift_rd_adr <= 1;
967
          incr_rd_digit_count <= 1;
968
          m1_next_state <= m1_rd_send_adr_sr;
969
        end
970
        else m1_next_state <= m1_rd_send_adr_sr;
971
      end
972
 
973
    m1_rd_send_separator :
974
      begin
975
        msg_base <= 5'b10000;    // Address of the separator message
976
        incr_msg_offset <= rs232_tx_load;
977
        if ((msg_offset == 2) && rs232_tx_load)
978
        begin
979
          m1_next_state <= m1_rd_send_dat_sr;
980
          reset_rd_digit_count <= 1;
981
          reset_msg_offset <= 1;
982
        end
983
        else m1_next_state <= m1_rd_send_separator;
984
      end
985
 
986
    m1_rd_send_dat_sr :
987
      begin
988
        msg_base <= {1'b0,dat_sr[`NIBBLE_SIZE*DAT_DIGITS_PP-1:
989
                                 `NIBBLE_SIZE*(DAT_DIGITS_PP-1)]};
990
        if (
991
            (rd_digit_count == DAT_DIGITS_PP-1)
992
            && (rd_field_count == RD_FIELDS_PP-1)
993
            && rs232_tx_load
994
            )
995
        begin
996
          m1_next_state <= m1_rd_send_crlf;
997
          reset_rd_field_count <= 1;
998
        end
999
        else if ((rd_digit_count == DAT_DIGITS_PP-1) && rs232_tx_load) begin
1000
          m1_next_state <= m1_rd_send_space;
1001
          incr_rd_field_count <= 1;
1002
        end
1003
        else if (rs232_tx_load) begin
1004
            store_dat <= 1;
1005
            incr_rd_digit_count <= 1;
1006
            m1_next_state <= m1_rd_send_dat_sr;
1007
        end
1008
        else m1_next_state <= m1_rd_send_dat_sr;
1009
      end
1010
 
1011
    m1_rd_send_space :
1012
      begin
1013
        msg_base <= 5'b10000;    // Address of the space
1014
        incr_msg_offset <= rs232_tx_load;
1015
        if ((msg_offset == 0) && rs232_tx_load) begin
1016
          m1_next_state <= m1_bus_granted;
1017
          reset_msg_offset <= 1;
1018
        end
1019
        else m1_next_state <= m1_rd_send_space;
1020
      end
1021
 
1022
    m1_rd_send_crlf :
1023
      begin
1024
        msg_base <= 5'b10111;     // Address of the cr/lf message
1025
        incr_msg_offset <= rs232_tx_load;
1026
        if ((msg_offset == 1) && rs232_tx_load) begin
1027
          m1_next_state <= m1_bus_granted;
1028
          reset_msg_offset <= 1;
1029
        end
1030
        else m1_next_state <= m1_rd_send_crlf;
1031
      end
1032
 
1033
    default : m1_next_state <= m1_initial_state;
1034
  endcase
1035
end
1036
 
1037
 
1038
// This is the counter for incrementing or loading the cmd_ptr
1039
always @(posedge clk_i)
1040
begin
1041
  if (reset_i || reset_cmd_ptr) cmd_ptr <= 0;
1042
  else if (decr_cmd_ptr) cmd_ptr <= cmd_ptr - 1;
1043
  else if (incr_cmd_ptr) cmd_ptr <= cmd_ptr + 1;
1044
end
1045
 
1046
 
1047
// This is the command buffer writing section
1048
always @(posedge clk_i)
1049
begin
1050
  if (rs232_echo && cmd_buffer_write) cmd_buffer[cmd_ptr] <= rs232_rx_char;
1051
end
1052
// This is the command buffer reading section
1053
assign cmd_char = cmd_buffer[cmd_ptr];
1054
assign lc_cmd_char = (cmd_buffer[cmd_ptr] | 8'h20); // lowercase
1055
 
1056
 
1057
 
1058
// These assigments are for detecting whether the cmd_char is
1059
// anything of special interest.
1060
assign char_is_enter = (cmd_char == 8'h0d);          // enter
1061
assign char_is_whitespace = (
1062
                                (cmd_char == 8'h20)  // space
1063
                             || (cmd_char == 8'h09)  // tab
1064
                             );
1065
assign char_is_num = ((cmd_char>=8'h30)&&(cmd_char<=8'h39));
1066
assign char_is_a_f = ((lc_cmd_char>=8'h61)&&(lc_cmd_char<=8'h66));
1067
assign char_is_hex = ( char_is_num || char_is_a_f );
1068
assign char_is_r = (lc_cmd_char == 8'h72); // "r"
1069
assign char_is_w = (lc_cmd_char == 8'h77); // "w"
1070
assign char_is_i = (lc_cmd_char == 8'h69); // "i"
1071
 
1072
assign hex_digit = char_is_num?cmd_char[3:0]:(cmd_char[3:0]+9);
1073
 
1074
// This is the command register.  It stores the type of command to execute.
1075
// This is so that the state machine can parse address, data and qty
1076
// into "generic" storage locations, and then when it executes the command,
1077
// it refers back to this register in order to determine what type of
1078
// operation to perform.
1079
 
1080
always @(posedge clk_i)
1081
begin
1082
  if (reset_i) command <= `CMD_0;
1083
  else if (cmd_i) command <= `CMD_I;
1084
  else if (cmd_r) command <= `CMD_R;
1085
  else if (cmd_w) command <= `CMD_W;
1086
end
1087
 
1088
// This is the "nibble" shift register for the address which is sent character
1089
// by character to the user.  It is loaded each time the adr_offset is
1090
// incremented, in order to save the previous address for use in printing
1091
// to the user.
1092
always @(posedge clk_i)
1093
begin
1094
  if (reset_i || reset_adr) rd_adr_sr <= 0;
1095
  else if (incr_adr_offset) rd_adr_sr <= adr_ptr;
1096
  else if (shift_rd_adr) begin
1097
    rd_adr_sr[`NIBBLE_SIZE*ADR_DIGITS_PP-1:`NIBBLE_SIZE] <=
1098
      rd_adr_sr[`NIBBLE_SIZE*(ADR_DIGITS_PP-1)-1:0];
1099
    rd_adr_sr[`NIBBLE_SIZE-1:0] <= {`NIBBLE_SIZE{1'b0}};
1100
  end
1101
end
1102
 
1103
// These are the "nibble" shift registers.  They handle loading the
1104
// hexadecimal digits from the command line.
1105
always @(posedge clk_i)
1106
begin
1107
  if (reset_i || reset_adr) adr_sr <= 0;
1108
  else if (store_adr) begin
1109
    adr_sr[`NIBBLE_SIZE*ADR_DIGITS_PP-1:`NIBBLE_SIZE] <=
1110
      adr_sr[`NIBBLE_SIZE*(ADR_DIGITS_PP-1)-1:0];
1111
    adr_sr[`NIBBLE_SIZE-1:0] <= hex_digit;
1112
  end
1113
end
1114
 
1115
always @(posedge clk_i)
1116
begin
1117
  if (reset_i || reset_dat) dat_sr <= 0;
1118
  else if (capture_dat) dat_sr <= dat_io;
1119
  else if (store_dat) begin
1120
    dat_sr[`NIBBLE_SIZE*DAT_DIGITS_PP-1:`NIBBLE_SIZE] <=
1121
      dat_sr[`NIBBLE_SIZE*(DAT_DIGITS_PP-1)-1:0];
1122
    dat_sr[`NIBBLE_SIZE-1:0] <= hex_digit;
1123
  end
1124
end
1125
 
1126
always @(posedge clk_i)
1127
begin
1128
  if (reset_i || reset_qty) qty_sr <= 0;
1129
  else if (init_qty) qty_sr <= 1;
1130
  else if (store_qty) begin
1131
    qty_sr[`NIBBLE_SIZE*QTY_DIGITS_PP-1:`NIBBLE_SIZE] <=
1132
      qty_sr[`NIBBLE_SIZE*(QTY_DIGITS_PP-1)-1:0];
1133
    qty_sr[`NIBBLE_SIZE-1:0] <= hex_digit;
1134
  end
1135
end
1136
 
1137
// This is the rd_digit_count counter.  It is used for counting digits
1138
// displayed of both the adr_sr and dat_sr, so it must be able to count up
1139
// to the extent of the larger of the two...
1140
always @(posedge clk_i)
1141
begin
1142
  if (reset_i || reset_rd_digit_count) rd_digit_count <= 0;
1143
  else if (incr_rd_digit_count) rd_digit_count <= rd_digit_count + 1;
1144
end
1145
 
1146
// This is the rd_field_count counter.  It is used for counting dat_sr fields
1147
// displayed per line.
1148
always @(posedge clk_i)
1149
begin
1150
  if (reset_i || reset_rd_field_count) rd_field_count <= 0;
1151
  else if (incr_rd_field_count) rd_field_count <= rd_field_count + 1;
1152
end
1153
 
1154
 
1155
// This is the watchdog timer counter
1156
// The watchdog timer is always "enabled" to operate.
1157
always @(posedge clk_i)
1158
begin
1159
  if (reset_i || reset_watchdog) watchdog_timer_count <= 0;
1160
  else if (~watchdog_timer_done)
1161
    watchdog_timer_count <= watchdog_timer_count + 1;
1162
end
1163
assign watchdog_timer_done = (watchdog_timer_count==WATCHDOG_TIMER_VALUE_PP);
1164
 
1165
 
1166
endmodule
1167
 
1168
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.