|
| 1 | +/*Copyright 2020-2021 T-Head Semiconductor Co., Ltd. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +// &Depend("cpu_cfig.h"); @17 |
| 17 | + |
| 18 | +// &ModuleBeg; @19 |
| 19 | +module aq_biu_apbif( |
| 20 | + apb_clk_en, |
| 21 | + apbif_araddr, |
| 22 | + apbif_arid, |
| 23 | + apbif_arprot, |
| 24 | + apbif_arready, |
| 25 | + apbif_arvalid, |
| 26 | + apbif_awaddr, |
| 27 | + apbif_awid, |
| 28 | + apbif_awprot, |
| 29 | + apbif_awready, |
| 30 | + apbif_awvalid, |
| 31 | + apbif_idle, |
| 32 | + apbif_rdata, |
| 33 | + apbif_rid, |
| 34 | + apbif_rready, |
| 35 | + apbif_rresp, |
| 36 | + apbif_rvalid, |
| 37 | + apbif_wdata, |
| 38 | + apbif_wready, |
| 39 | + apbif_wvalid, |
| 40 | + cp0_biu_icg_en, |
| 41 | + cpurst_b, |
| 42 | + forever_cpuclk, |
| 43 | + pad_yy_icg_scan_en, |
| 44 | + paddr, |
| 45 | + penable, |
| 46 | + perr_clint, |
| 47 | + perr_plic, |
| 48 | + pprot, |
| 49 | + prdata_clint, |
| 50 | + prdata_plic, |
| 51 | + pready_clint, |
| 52 | + pready_plic, |
| 53 | + psel_clint, |
| 54 | + psel_plic, |
| 55 | + pwdata, |
| 56 | + pwrite |
| 57 | +); |
| 58 | + |
| 59 | +// &Ports; @20 |
| 60 | +input apb_clk_en; |
| 61 | +input [39 :0] apbif_araddr; |
| 62 | +input [3 :0] apbif_arid; |
| 63 | +input [1 :0] apbif_arprot; |
| 64 | +input apbif_arvalid; |
| 65 | +input [39 :0] apbif_awaddr; |
| 66 | +input [3 :0] apbif_awid; |
| 67 | +input [1 :0] apbif_awprot; |
| 68 | +input apbif_awvalid; |
| 69 | +input apbif_rready; |
| 70 | +input [127:0] apbif_wdata; |
| 71 | +input apbif_wvalid; |
| 72 | +input cp0_biu_icg_en; |
| 73 | +input cpurst_b; |
| 74 | +input forever_cpuclk; |
| 75 | +input pad_yy_icg_scan_en; |
| 76 | +input perr_clint; |
| 77 | +input perr_plic; |
| 78 | +input [31 :0] prdata_clint; |
| 79 | +input [31 :0] prdata_plic; |
| 80 | +input pready_clint; |
| 81 | +input pready_plic; |
| 82 | +output apbif_arready; |
| 83 | +output apbif_awready; |
| 84 | +output apbif_idle; |
| 85 | +output [127:0] apbif_rdata; |
| 86 | +output [3 :0] apbif_rid; |
| 87 | +output [1 :0] apbif_rresp; |
| 88 | +output apbif_rvalid; |
| 89 | +output apbif_wready; |
| 90 | +output [31 :0] paddr; |
| 91 | +output penable; |
| 92 | +output [1 :0] pprot; |
| 93 | +output psel_clint; |
| 94 | +output psel_plic; |
| 95 | +output [31 :0] pwdata; |
| 96 | +output pwrite; |
| 97 | + |
| 98 | +// &Regs; @21 |
| 99 | +reg [1 :0] apb_cur_state; |
| 100 | +reg [1 :0] apb_next_state; |
| 101 | +reg [39 :0] apbif_addr; |
| 102 | +reg [31 :0] apbif_data; |
| 103 | +reg [3 :0] apbif_id; |
| 104 | +reg [1 :0] apbif_prot; |
| 105 | +reg apbif_write; |
| 106 | +reg [31 :0] rdata; |
| 107 | +reg resp_err; |
| 108 | +reg [3 :0] resp_id; |
| 109 | +reg resp_vld; |
| 110 | +reg resp_wt; |
| 111 | +reg [1 :0] sel; |
| 112 | + |
| 113 | +// &Wires; @22 |
| 114 | +wire apb_clk_en; |
| 115 | +wire apb_fsm_idle; |
| 116 | +wire apb_fsm_pend; |
| 117 | +wire apb_fsm_req; |
| 118 | +wire apb_fsm_wdata; |
| 119 | +wire [39 :0] apbif_araddr; |
| 120 | +wire [3 :0] apbif_arid; |
| 121 | +wire [1 :0] apbif_arprot; |
| 122 | +wire apbif_arready; |
| 123 | +wire apbif_arvalid; |
| 124 | +wire [39 :0] apbif_awaddr; |
| 125 | +wire [3 :0] apbif_awid; |
| 126 | +wire [1 :0] apbif_awprot; |
| 127 | +wire apbif_awready; |
| 128 | +wire apbif_awvalid; |
| 129 | +wire apbif_clk; |
| 130 | +wire apbif_clk_en; |
| 131 | +wire apbif_idle; |
| 132 | +wire [127:0] apbif_rdata; |
| 133 | +wire apbif_req; |
| 134 | +wire [39 :0] apbif_req_addr; |
| 135 | +wire [3 :0] apbif_req_id; |
| 136 | +wire [1 :0] apbif_req_prot; |
| 137 | +wire apbif_req_write; |
| 138 | +wire [3 :0] apbif_rid; |
| 139 | +wire apbif_rready; |
| 140 | +wire [1 :0] apbif_rresp; |
| 141 | +wire apbif_rvalid; |
| 142 | +wire [127:0] apbif_wdata; |
| 143 | +wire apbif_wready; |
| 144 | +wire apbif_wvalid; |
| 145 | +wire arsel; |
| 146 | +wire clint_hit; |
| 147 | +wire cmplt; |
| 148 | +wire cp0_biu_icg_en; |
| 149 | +wire cpurst_b; |
| 150 | +wire forever_cpuclk; |
| 151 | +wire pad_yy_icg_scan_en; |
| 152 | +wire [31 :0] paddr; |
| 153 | +wire penable; |
| 154 | +wire perr; |
| 155 | +wire perr_clint; |
| 156 | +wire perr_plic; |
| 157 | +wire plic_hit; |
| 158 | +wire [1 :0] pprot; |
| 159 | +wire [31 :0] prdata; |
| 160 | +wire [31 :0] prdata_clint; |
| 161 | +wire [31 :0] prdata_plic; |
| 162 | +wire pready_clint; |
| 163 | +wire pready_plic; |
| 164 | +wire psel; |
| 165 | +wire psel_clint; |
| 166 | +wire psel_plic; |
| 167 | +wire [31 :0] pwdata; |
| 168 | +wire pwrite; |
| 169 | +wire resp_grant; |
| 170 | +wire sel_none; |
| 171 | + |
| 172 | + |
| 173 | +parameter ADDRW = 40; |
| 174 | +//========================================================== |
| 175 | +// request arbiter |
| 176 | +//========================================================== |
| 177 | +assign arsel = apbif_arvalid; |
| 178 | +assign apbif_req = apbif_arvalid | apbif_awvalid; |
| 179 | +assign apbif_req_addr[ADDRW-1:0] = arsel ? apbif_araddr[ADDRW-1:0] : apbif_awaddr[ADDRW-1:0]; |
| 180 | +assign apbif_req_prot[1:0] = arsel ? apbif_arprot[1:0] : apbif_awprot[1:0]; |
| 181 | +assign apbif_req_id[3:0] = arsel ? apbif_arid[3:0] : apbif_awid[3:0]; |
| 182 | +assign apbif_req_write = !arsel; |
| 183 | + |
| 184 | +assign apbif_arready = apb_fsm_idle & apb_clk_en; |
| 185 | +assign apbif_awready = apb_fsm_idle & !arsel & apb_clk_en; |
| 186 | +assign apbif_wready = apb_fsm_wdata & apb_clk_en; |
| 187 | + |
| 188 | +//========================================================== |
| 189 | +// APB FSM |
| 190 | +//========================================================== |
| 191 | +parameter IDLE = 2'b00; |
| 192 | +parameter WDATA = 2'b01; |
| 193 | +parameter REQ = 2'b10; |
| 194 | +parameter PEND = 2'b11; |
| 195 | + |
| 196 | +always @(posedge apbif_clk or negedge cpurst_b) |
| 197 | +begin |
| 198 | + if(~cpurst_b) |
| 199 | + apb_cur_state[1:0] <= IDLE; |
| 200 | + else if (apb_clk_en) |
| 201 | + apb_cur_state[1:0] <= apb_next_state[1:0]; |
| 202 | +end |
| 203 | + |
| 204 | +// &CombBeg; @55 |
| 205 | +always @( apbif_wvalid |
| 206 | + or apbif_arvalid |
| 207 | + or cmplt |
| 208 | + or apb_cur_state |
| 209 | + or apbif_awvalid) |
| 210 | +begin |
| 211 | + case (apb_cur_state) |
| 212 | + IDLE: begin |
| 213 | + if (apbif_arvalid) |
| 214 | + apb_next_state = REQ; |
| 215 | + else if (apbif_awvalid) |
| 216 | + apb_next_state = WDATA; |
| 217 | + else |
| 218 | + apb_next_state = IDLE; |
| 219 | + end |
| 220 | + WDATA: begin |
| 221 | + if (apbif_wvalid) |
| 222 | + apb_next_state = REQ; |
| 223 | + else |
| 224 | + apb_next_state = WDATA; |
| 225 | + end |
| 226 | + REQ: begin |
| 227 | + apb_next_state = PEND; |
| 228 | + end |
| 229 | + PEND: begin |
| 230 | + if (cmplt) |
| 231 | + apb_next_state = IDLE; |
| 232 | + else |
| 233 | + apb_next_state = PEND; |
| 234 | + end |
| 235 | + default: apb_next_state = IDLE; |
| 236 | + endcase |
| 237 | +// &CombEnd; @82 |
| 238 | +end |
| 239 | + |
| 240 | +assign apb_fsm_idle = apb_cur_state == IDLE; |
| 241 | +assign apb_fsm_wdata = apb_cur_state == WDATA; |
| 242 | +assign apb_fsm_req = apb_cur_state == REQ; |
| 243 | +assign apb_fsm_pend = apb_cur_state == PEND; |
| 244 | + |
| 245 | +//========================================================== |
| 246 | +// APB content |
| 247 | +//========================================================== |
| 248 | +always @(posedge apbif_clk or negedge cpurst_b) |
| 249 | +begin |
| 250 | + if (!cpurst_b)begin |
| 251 | + apbif_addr[ADDRW-1:0] <= {ADDRW{1'b0}}; |
| 252 | + apbif_id[3:0] <= 4'b0; |
| 253 | + apbif_prot[1:0] <= 2'b0; |
| 254 | + apbif_write <= 1'b0; |
| 255 | + end |
| 256 | + else if (apb_clk_en & apb_fsm_idle & apbif_req)begin |
| 257 | + apbif_addr[ADDRW-1:0] <= apbif_req_addr[ADDRW-1:0]; |
| 258 | + apbif_id[3:0] <= apbif_req_id[3:0]; |
| 259 | + apbif_prot[1:0] <= apbif_req_prot[1:0]; |
| 260 | + apbif_write <= apbif_req_write; |
| 261 | + end |
| 262 | +end |
| 263 | + |
| 264 | +always @(posedge apbif_clk or negedge cpurst_b) |
| 265 | +begin |
| 266 | + if (!cpurst_b) |
| 267 | + apbif_data[31:0] <= 32'b0; |
| 268 | + else if (apbif_wvalid && apb_fsm_wdata && apb_clk_en) |
| 269 | + begin |
| 270 | + if (apbif_addr[3:2] == 2'b00) |
| 271 | + apbif_data[31:0] <= apbif_wdata[31:0]; |
| 272 | + else if (apbif_addr[3:2] == 2'b01) |
| 273 | + apbif_data[31:0] <= apbif_wdata[63:32]; |
| 274 | + else if (apbif_addr[3:2] == 2'b10) |
| 275 | + apbif_data[31:0] <= apbif_wdata[95:64]; |
| 276 | + else if (apbif_addr[3:2] == 2'b11) |
| 277 | + apbif_data[31:0] <= apbif_wdata[127:96]; |
| 278 | + end |
| 279 | +end |
| 280 | + |
| 281 | +//========================================================== |
| 282 | +// APB request |
| 283 | +//========================================================== |
| 284 | +`define PLIC_BASE_START 1'b0 |
| 285 | +`define CLINT_BASE_START 11'h400 |
| 286 | + |
| 287 | +assign plic_hit = (apbif_addr[26] == `PLIC_BASE_START); |
| 288 | +assign clint_hit = (apbif_addr[26:16] == `CLINT_BASE_START); |
| 289 | + |
| 290 | +assign psel = apb_fsm_req | apb_fsm_pend; |
| 291 | +assign psel_plic = psel & plic_hit; |
| 292 | +assign psel_clint = psel & clint_hit; |
| 293 | + |
| 294 | +assign pwrite = apbif_write; |
| 295 | +assign paddr[31:0] = apbif_addr[31:0]; |
| 296 | +assign pwdata[31:0] = apbif_data[31:0]; |
| 297 | +assign penable = apb_fsm_pend; |
| 298 | +assign pprot[1:0] = apbif_prot[1:0]; |
| 299 | + |
| 300 | +always @(posedge apbif_clk or negedge cpurst_b) |
| 301 | +begin |
| 302 | + if (!cpurst_b) |
| 303 | + sel[1:0] <= 2'b0; |
| 304 | + else if (apb_fsm_req) |
| 305 | + sel[1:0] <= {clint_hit,plic_hit}; |
| 306 | +end |
| 307 | + |
| 308 | +//================================================ |
| 309 | +// apb response |
| 310 | +//================================================ |
| 311 | +assign sel_none = !(|sel[1:0]); |
| 312 | + |
| 313 | +assign prdata[31:0] = {32{sel[0]}} & prdata_plic[31:0] | |
| 314 | + {32{sel[1]}} & prdata_clint[31:0]; |
| 315 | + |
| 316 | +assign perr = sel[0] & perr_plic | |
| 317 | + sel[1] & perr_clint | |
| 318 | + sel_none; |
| 319 | + |
| 320 | +assign cmplt = apb_fsm_pend & |
| 321 | + (sel[0] & pready_plic | |
| 322 | + sel[1] & pready_clint | |
| 323 | + sel_none); |
| 324 | +assign resp_grant = apbif_write | apbif_rready; |
| 325 | + |
| 326 | +always @(posedge apbif_clk or negedge cpurst_b) |
| 327 | +begin |
| 328 | + if (!cpurst_b) |
| 329 | + resp_vld <= 1'b0; |
| 330 | + else if (cmplt && apb_clk_en) |
| 331 | + resp_vld <= 1'b1; |
| 332 | + else if (resp_grant) |
| 333 | + resp_vld <= 1'b0; |
| 334 | +end |
| 335 | + |
| 336 | +always @(posedge apbif_clk or negedge cpurst_b) |
| 337 | +begin |
| 338 | + if (!cpurst_b)begin |
| 339 | + rdata[31:0] <= 32'b0; |
| 340 | + resp_err <= 1'b0; |
| 341 | + resp_id[3:0] <= 4'b0; |
| 342 | + resp_wt <= 1'b0; |
| 343 | + end |
| 344 | + else if (cmplt && apb_clk_en) begin |
| 345 | + rdata[31:0] <= prdata[31:0]; |
| 346 | + resp_err <= perr; |
| 347 | + resp_id[3:0] <= apbif_id[3:0]; |
| 348 | + resp_wt <= apbif_write; |
| 349 | + end |
| 350 | +end |
| 351 | + |
| 352 | +assign apbif_rvalid = !resp_wt && resp_vld; |
| 353 | +assign apbif_rdata[127:0] = {4{rdata[31:0]}}; |
| 354 | +assign apbif_rresp[1:0] = {resp_err,1'b0}; |
| 355 | +assign apbif_rid[3:0] = resp_id[3:0]; |
| 356 | + |
| 357 | +assign apbif_idle = apb_fsm_idle & !resp_vld; |
| 358 | + |
| 359 | +//========================================================== |
| 360 | +// ICG |
| 361 | +//========================================================== |
| 362 | +assign apbif_clk_en = apbif_req | !apb_fsm_idle | resp_vld; |
| 363 | + |
| 364 | +// &Instance("gated_clk_cell", "x_apbif_gated_clk"); @208 |
| 365 | +gated_clk_cell x_apbif_gated_clk ( |
| 366 | + .clk_in (forever_cpuclk ), |
| 367 | + .clk_out (apbif_clk ), |
| 368 | + .external_en (1'b0 ), |
| 369 | + .global_en (1'b1 ), |
| 370 | + .local_en (apbif_clk_en ), |
| 371 | + .module_en (cp0_biu_icg_en ), |
| 372 | + .pad_yy_icg_scan_en (pad_yy_icg_scan_en) |
| 373 | +); |
| 374 | + |
| 375 | +// &Connect(.clk_in (forever_cpuclk), @209 |
| 376 | +// .external_en (1'b0), @210 |
| 377 | +// .global_en (1'b1), @211 |
| 378 | +// .module_en (cp0_biu_icg_en), @212 |
| 379 | +// .local_en (apbif_clk_en), @213 |
| 380 | +// .clk_out (apbif_clk)); @214 |
| 381 | + |
| 382 | +//========================================================== |
| 383 | +// ICG |
| 384 | +//========================================================== |
| 385 | +// &ModuleEnd; @226 |
| 386 | +endmodule |
| 387 | + |
| 388 | + |
0 commit comments