]> rtime.felk.cvut.cz Git - fpga/zynq/canbench-sw.git/blob - system/ip/sja1000_1.0/hdl/can_ifc_axi_sync_duplex.v
580e3cd3851d289f87f6c01d073b7a71da4177ee
[fpga/zynq/canbench-sw.git] / system / ip / sja1000_1.0 / hdl / can_ifc_axi_sync_duplex.v
1 // synopsys translate_off
2 `include "timescale.v"
3 // synopsys translate_on
4 //`include "can_defines.v"
5
6         module can_ifc_axi_sync_duplex #
7         (
8                 // Users to add parameters here
9
10                 // User parameters ends
11                 // Do not modify the parameters beyond this line
12
13                 // Width of S_AXI data bus
14                 parameter integer C_S_AXI_DATA_WIDTH    = 32,
15                 // Width of S_AXI address bus
16                 parameter integer C_S_AXI_ADDR_WIDTH    = 8
17         )
18         (
19                 // Users to add ports here
20                 output wire reg_rst_o,
21                 output wire reg_re_o,
22                 output wire reg_we_o,
23                 output wire [7:0] reg_addr_read_o,
24                 output wire [7:0] reg_addr_write_o,
25                 output wire [7:0] reg_data_in_o,
26                 input wire  [7:0] reg_data_out_i,
27
28                 // User ports ends
29                 // Do not modify the ports beyond this line
30
31                 // Global Clock Signal
32                 input wire  S_AXI_ACLK,
33                 // Global Reset Signal. This Signal is Active LOW
34                 input wire  S_AXI_ARESETN,
35                 // Write address (issued by master, acceped by Slave)
36                 input wire [C_S_AXI_ADDR_WIDTH-1 : 0] S_AXI_AWADDR,
37                 // Write channel Protection type. This signal indicates the
38                 // privilege and security level of the transaction, and whether
39                 // the transaction is a data access or an instruction access.
40                 input wire [2 : 0] S_AXI_AWPROT,
41                 // Write address valid. This signal indicates that the master signaling
42                 // valid write address and control information.
43                 input wire  S_AXI_AWVALID,
44                 // Write address ready. This signal indicates that the slave is ready
45                 // to accept an address and associated control signals.
46                 output wire  S_AXI_AWREADY,
47                 // Write data (issued by master, acceped by Slave) 
48                 input wire [C_S_AXI_DATA_WIDTH-1 : 0] S_AXI_WDATA,
49                 // Write strobes. This signal indicates which byte lanes hold
50                 // valid data. There is one write strobe bit for each eight
51                 // bits of the write data bus.    
52                 input wire [(C_S_AXI_DATA_WIDTH/8)-1 : 0] S_AXI_WSTRB,
53                 // Write valid. This signal indicates that valid write
54                 // data and strobes are available.
55                 input wire  S_AXI_WVALID,
56                 // Write ready. This signal indicates that the slave
57                 // can accept the write data.
58                 output wire  S_AXI_WREADY,
59                 // Write response. This signal indicates the status
60                 // of the write transaction.
61                 output wire [1 : 0] S_AXI_BRESP,
62                 // Write response valid. This signal indicates that the channel
63                 // is signaling a valid write response.
64                 output wire  S_AXI_BVALID,
65                 // Response ready. This signal indicates that the master
66                 // can accept a write response.
67                 input wire  S_AXI_BREADY,
68                 // Read address (issued by master, acceped by Slave)
69                 input wire [C_S_AXI_ADDR_WIDTH-1 : 0] S_AXI_ARADDR,
70                 // Protection type. This signal indicates the privilege
71                 // and security level of the transaction, and whether the
72                 // transaction is a data access or an instruction access.
73                 input wire [2 : 0] S_AXI_ARPROT,
74                 // Read address valid. This signal indicates that the channel
75                 // is signaling valid read address and control information.
76                 input wire  S_AXI_ARVALID,
77                 // Read address ready. This signal indicates that the slave is
78                 // ready to accept an address and associated control signals.
79                 output wire  S_AXI_ARREADY,
80                 // Read data (issued by slave)
81                 output wire [C_S_AXI_DATA_WIDTH-1 : 0] S_AXI_RDATA,
82                 // Read response. This signal indicates the status of the
83                 // read transfer.
84                 output wire [1 : 0] S_AXI_RRESP,
85                 // Read valid. This signal indicates that the channel is
86                 // signaling the required read data.
87                 output wire  S_AXI_RVALID,
88                 // Read ready. This signal indicates that the master can
89                 // accept the read data and response information.
90                 input wire  S_AXI_RREADY
91         );
92
93         // AXI4LITE signals
94         reg [C_S_AXI_ADDR_WIDTH-1 : 0]  axi_awaddr;
95         reg     axi_awready;
96         reg     axi_wready;
97         reg [1 : 0]     axi_bresp;
98         reg     axi_bvalid;
99         reg [C_S_AXI_ADDR_WIDTH-1 : 0]  axi_araddr;
100         reg     axi_arready;
101         reg [C_S_AXI_DATA_WIDTH-1 : 0]  axi_rdata;
102         reg [1 : 0]     axi_rresp;
103         reg     axi_rvalid;
104
105         // Example-specific design signals
106         // local parameter for addressing 32 bit / 64 bit C_S_AXI_DATA_WIDTH
107         // ADDR_LSB is used for addressing 32/64 bit registers/memories
108         // ADDR_LSB = 2 for 32 bits (n downto 2)
109         // ADDR_LSB = 3 for 64 bits (n downto 3)
110         localparam integer ADDR_LSB = (C_S_AXI_DATA_WIDTH/32) + 1;
111         localparam integer OPT_MEM_ADDR_BITS = 1;
112         //----------------------------------------------
113         //-- Signals for user logic register space example
114         //------------------------------------------------
115         //-- Number of Slave Registers 4
116         /*reg [C_S_AXI_DATA_WIDTH-1:0]  slv_reg0;
117         reg [C_S_AXI_DATA_WIDTH-1:0]    slv_reg1;
118         reg [C_S_AXI_DATA_WIDTH-1:0]    slv_reg2;
119         reg [C_S_AXI_DATA_WIDTH-1:0]    slv_reg3;*/
120         wire     slv_reg_rden;
121         wire     slv_reg_wren;
122         //reg [C_S_AXI_DATA_WIDTH-1:0]   reg_data_out;
123         //integer        byte_index;
124
125         // I/O Connections assignments
126
127         assign S_AXI_AWREADY    = axi_awready;
128         assign S_AXI_WREADY     = axi_wready;
129         assign S_AXI_BRESP      = axi_bresp;
130         assign S_AXI_BVALID     = axi_bvalid;
131         assign S_AXI_ARREADY    = axi_arready;
132         assign S_AXI_RDATA      = axi_rdata;
133         assign S_AXI_RRESP      = axi_rresp;
134         assign S_AXI_RVALID     = axi_rvalid;
135         // Implement axi_awready generation
136         // axi_awready is asserted for one S_AXI_ACLK clock cycle when both
137         // S_AXI_AWVALID and S_AXI_WVALID are asserted. axi_awready is
138         // de-asserted when reset is low.
139
140         always @( posedge S_AXI_ACLK )
141         begin
142           if ( S_AXI_ARESETN == 1'b0 )
143             begin
144               axi_awready <= 1'b0;
145             end 
146           else
147             begin    
148               if (~axi_awready && S_AXI_AWVALID && S_AXI_WVALID)
149                 begin
150                   // slave is ready to accept write address when 
151                   // there is a valid write address and write data
152                   // on the write address and data bus. This design 
153                   // expects no outstanding transactions. 
154                   axi_awready <= 1'b1;
155                 end
156               else           
157                 begin
158                   axi_awready <= 1'b0;
159                 end
160             end 
161         end       
162
163         // Implement axi_awaddr latching
164         // This process is used to latch the address when both 
165         // S_AXI_AWVALID and S_AXI_WVALID are valid. 
166
167         always @( posedge S_AXI_ACLK )
168         begin
169           if ( S_AXI_ARESETN == 1'b0 )
170             begin
171               axi_awaddr <= 0;
172             end 
173           else
174             begin    
175               if (~axi_awready && S_AXI_AWVALID && S_AXI_WVALID)
176                 begin
177                   // Write Address latching 
178                   axi_awaddr <= S_AXI_AWADDR;
179                 end
180             end 
181         end       
182
183         // Implement axi_wready generation
184         // axi_wready is asserted for one S_AXI_ACLK clock cycle when both
185         // S_AXI_AWVALID and S_AXI_WVALID are asserted. axi_wready is 
186         // de-asserted when reset is low. 
187
188         always @( posedge S_AXI_ACLK )
189         begin
190           if ( S_AXI_ARESETN == 1'b0 )
191             begin
192               axi_wready <= 1'b0;
193             end 
194           else
195             begin    
196               if (~axi_wready && S_AXI_WVALID && S_AXI_AWVALID)
197                 begin
198                   // slave is ready to accept write data when 
199                   // there is a valid write address and write data
200                   // on the write address and data bus. This design 
201                   // expects no outstanding transactions. 
202                   axi_wready <= 1'b1;
203                 end
204               else
205                 begin
206                   axi_wready <= 1'b0;
207                 end
208             end 
209         end       
210
211         // Implement memory mapped register select and write logic generation
212         // The write data is accepted and written to memory mapped registers when
213         // axi_awready, S_AXI_WVALID, axi_wready and S_AXI_WVALID are asserted. Write strobes are used to
214         // select byte enables of slave registers while writing.
215         // These registers are cleared when reset (active low) is applied.
216         // Slave register write enable is asserted when valid address and data are available
217         // and the slave is ready to accept the write address and write data.
218         assign slv_reg_wren = axi_wready && S_AXI_WVALID && axi_awready && S_AXI_AWVALID;
219         /*
220         always @( posedge S_AXI_ACLK )
221         begin
222           if ( S_AXI_ARESETN == 1'b0 )
223             begin
224               slv_reg0 <= 0;
225               slv_reg1 <= 0;
226               slv_reg2 <= 0;
227               slv_reg3 <= 0;
228             end 
229           else begin
230             if (slv_reg_wren)
231               begin
232                 case ( axi_awaddr[ADDR_LSB+OPT_MEM_ADDR_BITS:ADDR_LSB] )
233                   2'h0:
234                     for ( byte_index = 0; byte_index <= (C_S_AXI_DATA_WIDTH/8)-1; byte_index = byte_index+1 )
235                       if ( S_AXI_WSTRB[byte_index] == 1 ) begin
236                         // Respective byte enables are asserted as per write strobes 
237                         // Slave register 0
238                         slv_reg0[(byte_index*8) +: 8] <= S_AXI_WDATA[(byte_index*8) +: 8];
239                       end  
240                   2'h1:
241                     for ( byte_index = 0; byte_index <= (C_S_AXI_DATA_WIDTH/8)-1; byte_index = byte_index+1 )
242                       if ( S_AXI_WSTRB[byte_index] == 1 ) begin
243                         // Respective byte enables are asserted as per write strobes 
244                         // Slave register 1
245                         slv_reg1[(byte_index*8) +: 8] <= S_AXI_WDATA[(byte_index*8) +: 8];
246                       end  
247                   2'h2:
248                     for ( byte_index = 0; byte_index <= (C_S_AXI_DATA_WIDTH/8)-1; byte_index = byte_index+1 )
249                       if ( S_AXI_WSTRB[byte_index] == 1 ) begin
250                         // Respective byte enables are asserted as per write strobes 
251                         // Slave register 2
252                         slv_reg2[(byte_index*8) +: 8] <= S_AXI_WDATA[(byte_index*8) +: 8];
253                       end  
254                   2'h3:
255                     for ( byte_index = 0; byte_index <= (C_S_AXI_DATA_WIDTH/8)-1; byte_index = byte_index+1 )
256                       if ( S_AXI_WSTRB[byte_index] == 1 ) begin
257                         // Respective byte enables are asserted as per write strobes 
258                         // Slave register 3
259                         slv_reg3[(byte_index*8) +: 8] <= S_AXI_WDATA[(byte_index*8) +: 8];
260                       end  
261                   default : begin
262                               slv_reg0 <= slv_reg0;
263                               slv_reg1 <= slv_reg1;
264                               slv_reg2 <= slv_reg2;
265                               slv_reg3 <= slv_reg3;
266                             end
267                 endcase
268               end
269           end
270         end    
271         */
272
273         // Implement write response logic generation
274         // The write response and response valid signals are asserted by the slave 
275         // when axi_wready, S_AXI_WVALID, axi_wready and S_AXI_WVALID are asserted.  
276         // This marks the acceptance of address and indicates the status of 
277         // write transaction.
278
279         always @( posedge S_AXI_ACLK )
280         begin
281           if ( S_AXI_ARESETN == 1'b0 )
282             begin
283               axi_bvalid  <= 0;
284               axi_bresp   <= 2'b0;
285             end 
286           else
287             begin    
288               if (axi_awready && S_AXI_AWVALID && ~axi_bvalid && axi_wready && S_AXI_WVALID)
289                 begin
290                   // indicates a valid write response is available
291                   axi_bvalid <= 1'b1;
292                   axi_bresp  <= 2'b0; // 'OKAY' response 
293                 end                   // work error responses in future
294               else
295                 begin
296                   if (S_AXI_BREADY && axi_bvalid) 
297                     //check if bready is asserted while bvalid is high) 
298                     //(there is a possibility that bready is always asserted high)   
299                     begin
300                       axi_bvalid <= 1'b0; 
301                     end  
302                 end
303             end
304         end   
305
306         // Implement axi_arready generation
307         // axi_arready is asserted for one S_AXI_ACLK clock cycle when
308         // S_AXI_ARVALID is asserted. axi_awready is 
309         // de-asserted when reset (active low) is asserted. 
310         // The read address is also latched when S_AXI_ARVALID is 
311         // asserted. axi_araddr is reset to zero on reset assertion.
312
313         always @( posedge S_AXI_ACLK )
314         begin
315           if ( S_AXI_ARESETN == 1'b0 )
316             begin
317               axi_arready <= 1'b0;
318               axi_araddr  <= 32'b0;
319             end 
320           else
321             begin    
322               if (~axi_arready && S_AXI_ARVALID)
323                 begin
324                   // indicates that the slave has acceped the valid read address
325                   axi_arready <= 1'b1;
326                   // Read address latching
327                   axi_araddr  <= S_AXI_ARADDR;
328                 end
329               else
330                 begin
331                   axi_arready <= 1'b0;
332                 end
333             end 
334         end       
335
336         // Implement axi_arvalid generation
337         // axi_rvalid is asserted for one S_AXI_ACLK clock cycle when both 
338         // S_AXI_ARVALID and axi_arready are asserted. The slave registers 
339         // data are available on the axi_rdata bus at this instance. The 
340         // assertion of axi_rvalid marks the validity of read data on the 
341         // bus and axi_rresp indicates the status of read transaction.axi_rvalid 
342         // is deasserted on reset (active low). axi_rresp and axi_rdata are 
343         // cleared to zero on reset (active low).  
344         always @( posedge S_AXI_ACLK )
345         begin
346           if ( S_AXI_ARESETN == 1'b0 )
347             begin
348               axi_rvalid <= 0;
349               axi_rresp  <= 0;
350             end 
351           else
352             begin    
353               if (axi_arready && S_AXI_ARVALID && ~axi_rvalid)
354                 begin
355                   // Valid read data is available at the read data bus
356                   axi_rvalid <= 1'b1;
357                   axi_rresp  <= 2'b0; // 'OKAY' response
358                 end   
359               else if (axi_rvalid && S_AXI_RREADY)
360                 begin
361                   // Read data is accepted by the master
362                   axi_rvalid <= 1'b0;
363                 end                
364             end
365         end    
366
367         // Implement memory mapped register select and read logic generation
368         // Slave register read enable is asserted when valid address is available
369         // and the slave is ready to accept the read address.
370         assign slv_reg_rden = axi_arready & S_AXI_ARVALID & ~axi_rvalid;
371         /*
372         always @(*)
373         begin
374               // Address decoding for reading registers
375               case ( axi_araddr[ADDR_LSB+OPT_MEM_ADDR_BITS:ADDR_LSB] )
376                 2'h0   : reg_data_out <= slv_reg0;
377                 2'h1   : reg_data_out <= slv_reg1;
378                 2'h2   : reg_data_out <= slv_reg2;
379                 2'h3   : reg_data_out <= slv_reg3;
380                 default : reg_data_out <= 0;
381               endcase
382         end
383         */
384
385         // Output register or memory read data
386         always @( posedge S_AXI_ACLK )
387         begin
388           if ( S_AXI_ARESETN == 1'b0 )
389             begin
390               axi_rdata  <= 0;
391             end 
392           else
393             begin    
394               // When there is a valid read address (S_AXI_ARVALID) with 
395               // acceptance of read address by the slave (axi_arready), 
396               // output the read dada 
397               if (slv_reg_rden)
398                 begin
399                   axi_rdata[7:0] <= reg_data_out_i;     // register read data
400                   axi_rdata[C_S_AXI_DATA_WIDTH-1 : 8] <= 0;
401                 end   
402             end
403         end    
404
405         // Add user logic here
406         assign reg_addr_read_o = S_AXI_ARADDR;
407         assign reg_addr_write_o= S_AXI_AWADDR;
408         assign reg_rst_o       = ~S_AXI_ARESETN;
409         assign reg_re_o        = slv_reg_rden;
410         assign reg_we_o        = slv_reg_wren;
411         assign reg_data_in_o   = S_AXI_WDATA[7:0];
412         //assign reg_data_out    = reg_data_out_i;
413
414         // User logic ends
415
416         endmodule