d7ff5f684924dd844a00e6c23c790ffe00618458
[shakti-peripherals.git] / src / peripherals / mux / mux.bsv
1 /*
2 Copyright (c) 2013, IIT Madras
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6
7 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9 * Neither the name of IIT Madras nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
11 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
13 */
14 package mux;
15 /*==== Package imports ==== */
16 import TriState ::*;
17 import Vector ::*;
18 import BUtils::*;
19 import ConfigReg ::*;
20 /*============================ */
21 /*===== Project Imports ===== */
22 import Semi_FIFOF :: *;
23 import AXI4_Lite_Types :: *;
24 /*============================ */
25 `include "instance_defines.bsv"
26
27 interface MUX_config#(numeric type ionum);
28 (*always_ready,always_enabled*)
29 method Vector#(ionum,Bit#(2)) mux;
30 endinterface
31
32 interface MUX#(numeric type ionum);
33 interface MUX_config#(ionum) mux_config;
34 interface AXI4_Lite_Slave_IFC#(`PADDR,`DATA,`USERSPACE) axi_slave;
35 endinterface
36
37 // (*synthesize*)
38 module mkmux(MUX#(ionum_));
39 Vector#(ionum_,ConfigReg#(Bit#(2))) muxer_reg <-replicateM(mkConfigReg(0));
40
41 AXI4_Lite_Slave_Xactor_IFC #(`PADDR, `DATA, `USERSPACE) s_xactor <- mkAXI4_Lite_Slave_Xactor;
42 let ionum=valueOf(ionum_);
43 rule rl_wr_respond;
44 // Get the wr request
45 //aw is write address, w is write data
46 let aw <- pop_o (s_xactor.o_wr_addr);
47 let w <- pop_o (s_xactor.o_wr_data);
48 let b = AXI4_Lite_Wr_Resp {bresp: AXI4_LITE_OKAY, buser: aw.awuser};
49 if(aw.awaddr[5:0]=='h0)
50 for(Integer i=0;i<min(ionum, 16);i=i+1) begin
51 muxer_reg[i]<= w.wdata[i*2+1:i*2];
52 end
53 else if(aw.awaddr[5:0]=='h4 && ionum>=16)
54 for(Integer i=0;i<ionum-16;i=i+1) begin
55 muxer_reg[i+16]<= w.wdata[i*2+1:i*2];
56 end
57 else
58 b.bresp=AXI4_LITE_SLVERR;
59 s_xactor.i_wr_resp.enq (b);
60 endrule
61
62 rule rl_rd_respond;
63 // Get the read request
64 //ar is read address, r is read data
65 let ar<- pop_o(s_xactor.o_rd_addr);
66 Bit#(32) temp=0;
67 AXI4_Lite_Rd_Data#(`DATA,`USERSPACE) r = AXI4_Lite_Rd_Data {rresp: AXI4_LITE_OKAY, rdata: ?, ruser: 0};
68 if(ar.araddr[5:0]=='h0)begin
69 for(Integer i=0;i<min(ionum, 16);i=i+1) begin
70 temp[i*2+ 1:i*2]=muxer_reg[i];
71 end
72 r.rdata=duplicate(temp);
73 end
74 else if(ar.araddr[5:0]=='h4 && ionum>=16)begin
75 for(Integer i=0;i<ionum-16;i=i+1) begin
76 temp[i*2+ 1:i*2]=muxer_reg[i+ 16];
77 end
78 r.rdata=duplicate(temp);
79 end
80 else
81 r.rresp=AXI4_LITE_SLVERR;
82 s_xactor.i_rd_data.enq(r);
83 endrule
84
85 interface axi_slave= s_xactor.axi_side;
86 interface mux_config=interface MUX_config
87 method Vector#(ionum,Bit#(2)) mux;
88 Vector#(ionum,Bit#(2)) temp;
89 for(Integer i=0;i<ionum;i=i+1)
90 temp[i]=pack(muxer_reg[i]);
91 return temp;
92 endmethod
93 endinterface;
94 endmodule
95
96 endpackage
97