MatchLib
AxiLiteSlaveToMem.h
1 /*
2  * Copyright (c) 2016-2019, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License")
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __LITESLAVETOMEM_H__
18 #define __LITESLAVETOMEM_H__
19 
20 #include <systemc.h>
21 #include <ac_reset_signal_is.h>
22 #include <ac_int.h>
23 #include <hls_globals.h>
24 #include <axi/axi4.h>
25 #include <mem_array.h>
26 
53 template <int capacity>
54 class AxiLiteSlaveToMem : public sc_module {
55  private:
56  typedef typename axi::axi4<axi::cfg::lite_nowstrb> axi_;
57 
58  static const int capacity_in_bytes = capacity;
59  static const int banks = 1; // Not templated - only support 1 bank for now
60 
61  typedef typename axi_::Data Data;
63 
64  Memarray memarray;
65 
66  public:
67  axi_::read::template slave<> if_rd;
68  axi_::write::template slave<> if_wr;
69 
70  sc_in<bool> reset_bar;
71  sc_in<bool> clk;
72 
73  SC_CTOR(AxiLiteSlaveToMem)
74  : if_rd("if_rd"), if_wr("if_wr"), reset_bar("reset_bar"), clk("clk") {
75  SC_THREAD(run);
76  sensitive << clk.pos();
77  async_reset_signal_is(reset_bar, false);
78  }
79 
80  protected:
81  void run() {
82  axi_::AddrPayload rd_addr_pld;
83  axi_::AddrPayload wr_addr_pld;
84  axi_::ReadPayload data_pld;
85  axi_::WritePayload write_pld;
86  axi_::WRespPayload resp_pld;
87  bool rd_resp_pend;
88  bool wr_resp_pend;
89 
90  if_rd.reset();
91  if_wr.reset();
92 
93  rd_resp_pend = false;
94  wr_resp_pend = false;
95 
96  #pragma hls_pipeline_init_interval 4
97  #pragma pipeline_stall_mode flush
98  while (1) {
99  wait();
100 
101  bool rd_resp_pend_local = rd_resp_pend;
102  bool wr_resp_pend_local = wr_resp_pend;
103  bool rd_mem;
104  bool wr_mem;
105 
106  if (!rd_resp_pend_local) {
107  rd_mem = (if_rd.nb_aread(rd_addr_pld));
108  if (rd_mem) {
109  data_pld.resp = axi_::Enc::XRESP::OKAY;
110  rd_resp_pend_local = true;
111  }
112  }
113 
114  if (!wr_resp_pend_local) {
115  wr_mem = if_wr.nb_wread(wr_addr_pld, write_pld);
116 
117  if (wr_mem) {
118  resp_pld.resp = axi_::Enc::XRESP::OKAY;
119  wr_resp_pend_local = true;
120  }
121  }
122 
123  if (rd_mem)
124  data_pld.data = memarray.read(rd_addr_pld.addr);
125  if (wr_mem)
126  memarray.write(wr_addr_pld.addr,0,write_pld.data);
127  if (rd_resp_pend_local) {
128  rd_resp_pend_local = !(if_rd.nb_rwrite(data_pld));
129  }
130 
131  if (wr_resp_pend_local) {
132  wr_resp_pend_local = !(if_wr.nb_bwrite(resp_pld));
133  }
134  rd_resp_pend = rd_resp_pend_local;
135  wr_resp_pend = wr_resp_pend_local;
136  }
137  }
138 };
139 
140 #endif
A struct composed of the signals associated with an AXI write response.
Definition: axi4.h:236
A struct composed of the signals associated with AXI write data.
Definition: axi4.h:279
A struct composed of the signals associated with AXI read and write requests.
Definition: axi4.h:108
A struct composed of the signals associated with an AXI read response.
Definition: axi4.h:180
The base axi4 class parameterized according a valid config.
Definition: axi4.h:58
An AXI slave SRAM for the AXI-Lite protocol.