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

Subversion Repositories oms8051mini

[/] [oms8051mini/] [trunk/] [rtl/] [msg_handler/] [msg_handler.v] - Blame information for rev 37

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

Line No. Rev Author Line
1 6 dinesha
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  UART Message Handler Module                                 ////
4
////                                                              ////
5
////  This file is part of the oms8051mini cores project          ////
6
////  http://www.opencores.org/cores/oms8051min/                  ////
7
////                                                              ////
8
////  Description                                                 ////
9
////  Uart Message Handler definitions.                           ////
10
////                                                              ////
11
////  To Do:                                                      ////
12
////    nothing                                                   ////
13
////                                                              ////
14
////  Author(s):                                                  ////
15
////      - Dinesh Annayya, dinesha@opencores.org                 ////
16
////                                                              ////
17 19 dinesha
//////////////////////////////////////////////////////////////////////
18 36 dinesha
////  Revision:                                                   
19
////  v-0.0: 27 Nov 2016                                            
20
////       A. rtl file picked from                                
21
////           http://www.opencores.org/cores/uart2spi/           
22
////  v-0.1: 19 Jan 2017
23
////       A. Lint warning fixed for case statement      
24 6 dinesha
//////////////////////////////////////////////////////////////////////
25
////                                                              ////
26
//// Copyright (C) 2000 Authors and OPENCORES.ORG                 ////
27
////                                                              ////
28
//// This source file may be used and distributed without         ////
29
//// restriction provided that this copyright statement is not    ////
30
//// removed from the file and that any derivative work contains  ////
31
//// the original copyright notice and the associated disclaimer. ////
32
////                                                              ////
33
//// This source file is free software; you can redistribute it   ////
34
//// and/or modify it under the terms of the GNU Lesser General   ////
35
//// Public License as published by the Free Software Foundation; ////
36
//// either version 2.1 of the License, or (at your option) any   ////
37
//// later version.                                               ////
38
////                                                              ////
39
//// This source is distributed in the hope that it will be       ////
40
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
41
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
42
//// PURPOSE.  See the GNU Lesser General Public License for more ////
43
//// details.                                                     ////
44
////                                                              ////
45
//// You should have received a copy of the GNU Lesser General    ////
46
//// Public License along with this source; if not, download it   ////
47
//// from http://www.opencores.org/lgpl.shtml                     ////
48
////                                                              ////
49
//////////////////////////////////////////////////////////////////////
50
 
51 19 dinesha
module msg_handler (
52 6 dinesha
        reset_n ,
53
        sys_clk ,
54
 
55
 
56
    // UART-TX Information
57
        tx_data_avail,
58
        tx_rd,
59
        tx_data,
60
 
61
 
62
    // UART-RX Information
63
        rx_ready,
64
        rx_wr,
65
        rx_data,
66
 
67
      // Towards Register Interface
68
        reg_addr,
69
        reg_wr,
70
        reg_wdata,
71
        reg_req,
72
        reg_ack,
73
        reg_rdata
74
 
75
     );
76
 
77
 
78
// Define the Message Hanlde States
79
`define IDLE             4'h0
80
`define IDLE_TX_MSG1     4'h1
81
`define IDLE_TX_MSG2     4'h2
82
`define RX_CMD_PHASE     4'h3
83
`define WR_ADR_PHASE     4'h4
84
`define WR_DATA_PHASE    4'h5
85
`define SEND_WR_REQ      4'h6
86
`define RD_ADDR_PHASE    4'h7
87
`define SEND_RD_REQ      4'h8
88
`define SEND_RD_DATA     4'h9
89
`define TX_MSG           4'hA
90
 
91
`define BREAK_CHAR       8'h0A
92
 
93
//---------------------------------
94
// Global Dec
95
// ---------------------------------
96
 
97
input        reset_n               ; // line reset
98
input        sys_clk               ; // line clock
99
 
100
 
101
//--------------------------------------
102
// UART TXD Path
103
// -------------------------------------
104
output         tx_data_avail        ; // Indicate valid TXD Data available
105
output [7:0]   tx_data              ; // TXD Data to be transmited
106
input          tx_rd                ; // Indicate TXD Data Been Read
107
 
108
 
109
//--------------------------------------
110
// UART RXD Path
111
// -------------------------------------
112
output         rx_ready            ; // Indicate Ready to accept the Read Data
113
input [7:0]    rx_data             ; // RXD Data 
114
input          rx_wr               ; // Valid RXD Data
115
 
116
//---------------------------------------
117
// Control Unit interface
118
// --------------------------------------
119
 
120
output  [15:0] reg_addr           ; // Operend-1
121 19 dinesha
output  [7:0]  reg_wdata          ; // Operend-2
122 6 dinesha
output         reg_req            ; // Register Request
123
output         reg_wr             ; // 1 -> write; 0 -> read
124
input          reg_ack            ; // Register Ack
125 19 dinesha
input   [7:0]  reg_rdata          ;
126 6 dinesha
 
127
// Local Wire/Register Decleration
128
//
129
//
130
reg             tx_data_avail      ;
131
reg [7:0]       tx_data            ;
132
reg [16*8-1:0]  TxMsgBuf           ; // 16 Byte Tx Message Buffer
133
reg  [4:0]      TxMsgSize          ;
134
reg  [4:0]      RxMsgCnt           ; // Count the Receive Message Count
135
reg  [3:0]      State              ;
136
reg  [3:0]      NextState          ;
137
reg  [15:0]     cmd                ; // command
138
reg  [15:0]     reg_addr           ; // reg_addr
139 19 dinesha
reg  [7:0]      reg_wdata          ; // reg_addr
140 6 dinesha
reg             reg_wr             ; // 1 -> Reg Write request, 0 -> Read Requestion
141
reg             reg_req            ; // 1 -> Register request
142
 
143
 
144
wire rx_ready = 1;
145
/****************************************************************
146
*  UART Message Hanlding Steps
147
*
148
*  1. On Reset Or Unknown command, Send the Default Message
149
*     Select Option:
150
*     wr <addr> <data>
151
*     rd <addr>
152
*  2. Wait for User command <wr/rd>
153
*  3. On <wr> command move to write address phase;
154
*  phase
155
*       A. After write address phase move to write data phase
156
*       B. After write data phase, once user press \r command ; send register req
157
*          and write request and address + data
158
*       C. On receiving register ack response; send <success> message back and move
159
*          to state-2
160
*  3.  On <rd> command move to read address phase;
161
*       A. After read address phase , once user press '\r' command; send
162
*          register req , read request
163
*       C. On receiving register ack response; send <response + read_data> message and move
164
*          to state-2
165
*  *****************************************************************/
166
 
167
always @(negedge reset_n or posedge sys_clk)
168
begin
169
   if(reset_n == 1'b0) begin
170
      tx_data_avail <= 0;
171
      reg_req       <= 0;
172
      State         <= `IDLE;
173
      NextState     <= `IDLE;
174 36 dinesha
      TxMsgBuf      <= 0;
175
      TxMsgSize     <= 0;
176
      RxMsgCnt      <= 0;
177
      reg_addr      <= 0;
178
      reg_wdata     <= 0;
179
      reg_wr        <= 1'b0;
180
      reg_req       <= 1'b0;
181
      tx_data       <= 0;
182
      cmd           <= 0 ;
183 6 dinesha
   end else begin
184
   case(State)
185
      // Send Default Message
186
      `IDLE: begin
187
          TxMsgBuf      <= "Command Format:\n";  // Align to 16 character format by appending space character
188
          TxMsgSize     <= 16;
189
          tx_data_avail <= 0;
190
          State         <= `TX_MSG;
191
          NextState     <= `IDLE_TX_MSG1;
192
       end
193
 
194
      // Send Default Message (Contd..)
195
      `IDLE_TX_MSG1: begin
196
           TxMsgBuf      <= "wm <ad> <data>\n "; // Align to 16 character format by appending space character 
197
           TxMsgSize     <= 15;
198
           tx_data_avail <= 0;
199
           State         <= `TX_MSG;
200
           NextState     <= `IDLE_TX_MSG2;
201
        end
202
 
203
      // Send Default Message (Contd..)
204
      `IDLE_TX_MSG2: begin
205
           TxMsgBuf      <= "rm <ad>\n>>      ";  // Align to 16 character format by appending space character
206
           TxMsgSize     <= 10;
207
           tx_data_avail <= 0;
208
           RxMsgCnt      <= 0;
209
           State         <= `TX_MSG;
210
           NextState     <= `RX_CMD_PHASE;
211
      end
212
 
213
       // Wait for Response
214
    `RX_CMD_PHASE: begin
215
        if(rx_wr == 1) begin
216
           //if(RxMsgCnt == 0 && rx_data == " ") begin // Ignore the same
217
           if(RxMsgCnt == 0 && rx_data == 8'h20) begin // Ignore the same
218
           //end else if(RxMsgCnt > 0 && rx_data == " ") begin // Check the command
219
           end else if(RxMsgCnt > 0 && rx_data == 8'h20) begin // Check the command
220
             //if(cmd == "wm") begin
221
             if(cmd == 16'h776D) begin
222
                 RxMsgCnt <= 0;
223
                 reg_addr <= 0;
224
                 reg_wdata <= 0;
225
                 State <= `WR_ADR_PHASE;
226
              //end else if(cmd == "rm") begin
227
              end else if(cmd == 16'h726D) begin
228
                 reg_addr <= 0;
229
                 RxMsgCnt <= 0;
230
                 State <= `RD_ADDR_PHASE;
231
             end else begin // Unknow command
232
                State         <= `IDLE;
233
             end
234
           //end else if(rx_data == "\n") begin // Error State
235
           end else if(rx_data == `BREAK_CHAR) begin // Error State
236
              State         <= `IDLE;
237
           end
238
           else begin
239
              cmd <=  (cmd << 8) | rx_data ;
240
              RxMsgCnt <= RxMsgCnt+1;
241
           end
242
        end
243
     end
244
       // Write Address Phase 
245
    `WR_ADR_PHASE: begin
246
        if(rx_wr == 1) begin
247
           //if(RxMsgCnt == 0 && rx_data == " ") begin // Ignore the Space character
248
           if(RxMsgCnt == 0 && rx_data == 8'h20) begin // Ignore the Space character
249
           //end else if(RxMsgCnt > 0 && rx_data == " ") begin // Move to write data phase
250
           end else if(RxMsgCnt > 0 && rx_data == 8'h20) begin // Move to write data phase
251
              State         <= `WR_DATA_PHASE;
252
           //end else if(rx_data == "\n") begin // Error State
253
           end else if(rx_data == `BREAK_CHAR) begin // Error State
254
              State         <= `IDLE;
255
           end else begin
256
              reg_addr <= (reg_addr << 4) | char2hex(rx_data);
257
              RxMsgCnt <= RxMsgCnt+1;
258
           end
259
        end
260
     end
261
    // Write Data Phase 
262
    `WR_DATA_PHASE: begin
263
        if(rx_wr == 1) begin
264
           //if(rx_data == " ") begin // Ignore the Space character
265
           if(rx_data == 8'h20) begin // Ignore the Space character
266
           //end else if(rx_data == "\n") begin // Error State
267
           end else if(rx_data == `BREAK_CHAR) begin // Error State
268
              State           <= `SEND_WR_REQ;
269
              reg_wr          <= 1'b1; // Write request
270
              reg_req         <= 1'b1;
271
           end else begin // A to F
272 19 dinesha
              reg_wdata <= (reg_wdata << 4) | char2hex(rx_data);
273 6 dinesha
           end
274
        end
275
     end
276
    `SEND_WR_REQ: begin
277
        if(reg_ack)  begin
278
           reg_req       <= 1'b0;
279
           TxMsgBuf      <= "cmd success\n>>  "; // Align to 16 character format by appending space character 
280
           TxMsgSize     <= 14;
281
           tx_data_avail <= 0;
282
           State         <= `TX_MSG;
283
           NextState     <= `RX_CMD_PHASE;
284
       end
285
    end
286
 
287
       // Write Address Phase 
288
    `RD_ADDR_PHASE: begin
289
        if(rx_wr == 1) begin
290
           //if(rx_data == " ") begin // Ignore the Space character
291
           if(rx_data == 8'h20) begin // Ignore the Space character
292
           //end else if(rx_data == "\n") begin // Error State
293
           end else if(rx_data == `BREAK_CHAR) begin // Error State
294
              State           <= `SEND_RD_REQ;
295
              reg_wr          <= 1'b0; // Read request
296
              reg_req         <= 1'b1; // Reg Request
297
           end
298
           else begin // A to F
299
                 reg_addr     <= (reg_addr << 4) | char2hex(rx_data);
300
                 RxMsgCnt <= RxMsgCnt+1;
301
              end
302
           end
303
        end
304
 
305
    `SEND_RD_REQ: begin
306
        if(reg_ack)  begin
307
           reg_req       <= 1'b0;
308
           TxMsgBuf      <= "Response:       "; // Align to 16 character format by appending space character 
309
           TxMsgSize     <= 10;
310
           tx_data_avail <= 0;
311
           State         <= `TX_MSG;
312
           NextState     <= `SEND_RD_DATA;
313
       end
314
    end
315
    `SEND_RD_DATA: begin // Wait for Operation Completion
316
           TxMsgBuf[10*8-1:9*8]  <= hex2char(reg_rdata[7:4]);
317
           TxMsgBuf[9*8-1:8*8]   <= hex2char(reg_rdata[3:0]);
318
           TxMsgBuf[8*8-1:7*8]   <= "\n";
319 19 dinesha
           TxMsgSize     <= 3;
320 6 dinesha
           tx_data_avail <= 0;
321
           State         <= `TX_MSG;
322
           NextState     <= `RX_CMD_PHASE;
323
     end
324
 
325
       // Send Default Message (Contd..)
326
    `TX_MSG: begin
327
           tx_data_avail    <= 1;
328
           tx_data          <= TxMsgBuf[16*8-1:15*8];
329
           if(TxMsgSize == 0) begin
330
              tx_data_avail <= 0;
331
              State         <= NextState;
332
           end else if(tx_rd) begin
333
              TxMsgBuf      <= TxMsgBuf << 8;
334
              TxMsgSize     <= TxMsgSize -1;
335
           end
336
        end
337 36 dinesha
     default: begin
338
        State         <= `IDLE;
339
        NextState     <= `IDLE;
340
     end
341 6 dinesha
   endcase
342
   end
343
end
344
 
345
 
346
// Character to hex number
347
function [3:0] char2hex;
348
input [7:0] data_in;
349
case (data_in)
350
     8'h30:     char2hex = 4'h0; // character '0' 
351
     8'h31:     char2hex = 4'h1; // character '1'
352
     8'h32:     char2hex = 4'h2; // character '2'
353
     8'h33:     char2hex = 4'h3; // character '3'
354
     8'h34:     char2hex = 4'h4; // character '4' 
355
     8'h35:     char2hex = 4'h5; // character '5'
356
     8'h36:     char2hex = 4'h6; // character '6'
357
     8'h37:     char2hex = 4'h7; // character '7'
358
     8'h38:     char2hex = 4'h8; // character '8'
359
     8'h39:     char2hex = 4'h9; // character '9'
360
     8'h41:     char2hex = 4'hA; // character 'A'
361
     8'h42:     char2hex = 4'hB; // character 'B'
362
     8'h43:     char2hex = 4'hC; // character 'C'
363
     8'h44:     char2hex = 4'hD; // character 'D'
364
     8'h45:     char2hex = 4'hE; // character 'E'
365
     8'h46:     char2hex = 4'hF; // character 'F'
366
     8'h61:     char2hex = 4'hA; // character 'a'
367
     8'h62:     char2hex = 4'hB; // character 'b'
368
     8'h63:     char2hex = 4'hC; // character 'c'
369
     8'h64:     char2hex = 4'hD; // character 'd'
370
     8'h65:     char2hex = 4'hE; // character 'e'
371
     8'h66:     char2hex = 4'hF; // character 'f'
372
      default :  char2hex = 4'hF;
373
   endcase
374
endfunction
375
 
376
// Hex to Asci Character 
377
function [7:0] hex2char;
378
input [3:0] data_in;
379
case (data_in)
380
     4'h0:      hex2char = 8'h30; // character '0' 
381
     4'h1:      hex2char = 8'h31; // character '1'
382
     4'h2:      hex2char = 8'h32; // character '2'
383
     4'h3:      hex2char = 8'h33; // character '3'
384
     4'h4:      hex2char = 8'h34; // character '4' 
385
     4'h5:      hex2char = 8'h35; // character '5'
386
     4'h6:      hex2char = 8'h36; // character '6'
387
     4'h7:      hex2char = 8'h37; // character '7'
388
     4'h8:      hex2char = 8'h38; // character '8'
389
     4'h9:      hex2char = 8'h39; // character '9'
390
     4'hA:      hex2char = 8'h41; // character 'A'
391
     4'hB:      hex2char = 8'h42; // character 'B'
392
     4'hC:      hex2char = 8'h43; // character 'C'
393
     4'hD:      hex2char = 8'h44; // character 'D'
394
     4'hE:      hex2char = 8'h45; // character 'E'
395
     4'hF:      hex2char = 8'h46; // character 'F'
396
   endcase
397
endfunction
398
endmodule

powered by: WebSVN 2.1.0

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