swear01

Untitled

Dec 10th, 2023
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.68 KB | None | 0 0
  1. //----------------------------- DO NOT MODIFY THE I/O INTERFACE!! ------------------------------//
  2. module CHIP #( //
  3. parameter BIT_W = 32 //
  4. )( //
  5. // clock //
  6. input i_clk, //
  7. input i_rst_n, //
  8. // instruction memory //
  9. input [BIT_W-1:0] i_IMEM_data, //
  10. output [BIT_W-1:0] o_IMEM_addr, //
  11. output o_IMEM_cen, //
  12. // data memory //
  13. input i_DMEM_stall, //
  14. input [BIT_W-1:0] i_DMEM_rdata, //
  15. output o_DMEM_cen, //
  16. output o_DMEM_wen, //
  17. output [BIT_W-1:0] o_DMEM_addr, //
  18. output [BIT_W-1:0] o_DMEM_wdata, //
  19. // finnish procedure //
  20. output o_finish, //
  21. // cache //
  22. input i_cache_finish, //
  23. output o_proc_finish //
  24. ); //
  25. //----------------------------- DO NOT MODIFY THE I/O INTERFACE!! ------------------------------//
  26.  
  27. // ------------------------------------------------------------------------------------------------------------------------------------------------------
  28. // Parameters
  29. // ------------------------------------------------------------------------------------------------------------------------------------------------------
  30.  
  31. // TODO: any declaration
  32. parameter AUIPC = 7'b001011;
  33. parameter JAL = 7'b1101111;
  34. parameter JALR = 7'b1100111;
  35. parameter BRANCH = 7'b1100011;
  36.  
  37. // ------------------------------------------------------------------------------------------------------------------------------------------------------
  38. // Wires and Registers
  39.  
  40. // ------------------------------------------------------------------------------------------------------------------------------------------------------
  41.  
  42. // TODO: any declaration
  43. reg [BIT_W-1:0] PC, next_PC;
  44. wire mem_cen, mem_wen;
  45. wire [BIT_W-1:0] mem_addr, mem_wdata, mem_rdata;
  46. wire mem_stall;
  47. //
  48. reg [BIT_W-1:0] instruction;
  49. reg [BIT_W-1:0] imm;
  50. reg wen, zero, geq, branchsuccess;
  51. reg [BIT_W-1:0] wdata;
  52. wire [BIT_W-1:0]rdata1, rdata2;
  53. reg [BIT_W-1:0] alures, aludata1, aludata2; // alu
  54. // control
  55. wire s_branch, s_memwrite, s_memload, s_alusrc, s_auipc, s_jal, s_jalr, s_wen;
  56. wire [2:0] s_aluop;
  57. //
  58. reg [31:0] fetch_addr;
  59. reg fetch_cen;
  60. reg o_DMEM_wen_buf, o_DMEM_cen_buf ;
  61.  
  62. // ------------------------------------------------------------------------------------------------------------------------------------------------------
  63. // Continuous Assignment
  64. // ------------------------------------------------------------------------------------------------------------------------------------------------------
  65.  
  66. // TODO: any wire assignment
  67. assign o_IMEM_addr = fetch_addr;
  68. assign o_IMEM_cen = fetch_cen;
  69. assign o_DMEM_wdata = rdata2 ;
  70. assign o_DMEM_addr = alures ;
  71. assign o_DMEM_wen = o_DMEM_wen_buf ;
  72. assign o_DMEM_cen = o_DMEM_cen_buf ;
  73.  
  74. // ------------------------------------------------------------------------------------------------------------------------------------------------------
  75. // Submoddules
  76. // ------------------------------------------------------------------------------------------------------------------------------------------------------
  77.  
  78. // TODO: Reg_file wire connection
  79. Reg_file reg0(
  80. .i_clk (i_clk),
  81. .i_rst_n(i_rst_n),
  82. .wen (s_wen),
  83. .rs1 (instruction[19:15]),
  84. .rs2 (instruction[24:20]),
  85. .rd (instruction[11:7]),
  86. .wdata (wdata),
  87. .rdata1 (rdata1),
  88. .rdata2 (rdata2)
  89. );
  90.  
  91. controller con(
  92. .inst (instruction[6:0]),
  93. .func3 (instruction[14:12]),
  94. .func7 (instruction[31:25]),
  95. .branch (s_branch),
  96. .mem_write (s_memwrite),
  97. .mem_load (s_memload),
  98. .rs1_pc (s_auipc),
  99. .rs2_imm (s_alusrc),
  100. .ALUop (s_aluop),
  101. .MULop (s_mulop),
  102. .jump (s_jal),
  103. .jal_register (s_jalr),
  104. .reg_write(s_wen)
  105. );
  106.  
  107. // ------------------------------------------------------------------------------------------------------------------------------------------------------
  108. // Always Blocks
  109. // ------------------------------------------------------------------------------------------------------------------------------------------------------
  110.  
  111. // Todo: any combinational/sequential circuit
  112.  
  113. always @(*) begin
  114. fetch_addr = PC;
  115. fetch_cen = 1;
  116. instruction = i_IMEM_data;
  117. end
  118.  
  119. always @(*) begin // Immgen
  120. case(instruction[6:0])
  121. JAL: begin
  122. imm[20] = instruction[31];
  123. imm[10:1] = instruction[30:21];
  124. imm[11] = instruction[20];
  125. imm[19:12] = instruction[19:12];
  126. imm[31:21] = 0;
  127. end
  128. JALR: begin
  129. imm[11:0] = instruction[31:20];
  130. imm[31:12] = 0;
  131. end
  132. BRANCH: begin
  133. imm[12] = instruction[31];
  134. imm[10:5] = instruction[30:25];
  135. imm[4:1] = instruction[11:8];
  136. imm[11] = instruction[7];
  137. imm[31:13] = 0;
  138. imm[0] = 0;
  139. end
  140. AUIPC: begin
  141. imm[31:12] = instruction[31:12];
  142. imm[11:0] = 0;
  143. end
  144. default: begin // I-type
  145. imm[11:0] = instruction[31:20];
  146. imm[31:12] = 0;
  147. end
  148. endcase
  149. end
  150.  
  151. always @(*) begin // branch/jump selection
  152. if(s_jal) begin
  153. next_PC = PC + (imm << 1);
  154. end
  155. else if(s_jalr) begin
  156. next_PC = imm + rdata1;
  157. end
  158. else begin
  159. case(instruction[14:12])
  160. 000: branchsuccess = zero; // beq
  161. 001: branchsuccess = !zero; // bne
  162. 100: branchsuccess = !geq; // blt
  163. 101: branchsuccess = geq; // bge
  164. default: branchsuccess = 0;
  165. endcase
  166. next_PC = (s_branch & branchsuccess) ? PC + 4 : PC + (imm << 1);
  167. end
  168. end
  169.  
  170. always @(*) begin // alu
  171. aludata1 = (s_auipc) ? PC : rdata1;
  172. aludata2 = (s_alusrc) ? imm : rdata2;
  173. case(s_aluop)
  174. 3'd0: alures = aludata1 + aludata2;
  175. 3'd1: alures = aludata1 - aludata2;
  176. 3'd2: alures = aludata1 & aludata2;
  177. 3'd3: alures = aludata1 ^ aludata2;
  178. 3'd4: alures = aludata1 << aludata2;
  179. 3'd5: alures = aludata1 >> aludata2;
  180. 3'd6: alures = (aludata1 < aludata2) ? 1 : 0;
  181. default: alures = 0;
  182. endcase
  183. zero = (alures == 0);
  184. geq = !(alures[31]);
  185. end
  186.  
  187. always @(alures) begin
  188. // write
  189. if (s_memwrite) begin
  190. o_DMEM_wen_buf = 1 ;
  191. o_DMEM_cen_buf = 1 ;
  192. end
  193. else if (s_memload) begin
  194. o_DMEM_wen_buf = 0 ;
  195. o_DMEM_cen_buf = 1 ;
  196. end
  197. else begin
  198. o_DMEM_wen_buf = 0 ;
  199. o_DMEM_cen_buf = 0 ;
  200. end
  201. end
  202.  
  203. always @(posedge i_clk or negedge i_rst_n) begin
  204. if (!i_rst_n) begin
  205. PC <= 32'h00010000; // Do not modify this value!!!
  206. end
  207. else begin
  208. PC <= next_PC;
  209. end
  210. end
  211.  
  212. endmodule
  213.  
  214. module Reg_file(i_clk, i_rst_n, wen, rs1, rs2, rd, wdata, rdata1, rdata2);
  215.  
  216. parameter BITS = 32;
  217. parameter word_depth = 32;
  218. parameter addr_width = 5; // 2^addr_width >= word_depth
  219.  
  220. input i_clk, i_rst_n, wen; // wen: 0:read | 1:write
  221. input [BITS-1:0] wdata;
  222. input [addr_width-1:0] rs1, rs2, rd;
  223.  
  224. output [BITS-1:0] rdata1, rdata2;
  225.  
  226. reg [BITS-1:0] mem [0:word_depth-1];
  227. reg [BITS-1:0] mem_nxt [0:word_depth-1];
  228.  
  229. integer i;
  230.  
  231. assign rdata1 = mem[rs1];
  232. assign rdata2 = mem[rs2];
  233.  
  234. always @(*) begin
  235. for (i=0; i<word_depth; i=i+1)
  236. mem_nxt[i] = (wen && (rd == i)) ? wdata : mem[i];
  237. end
  238.  
  239. always @(posedge i_clk or negedge i_rst_n) begin
  240. if (!i_rst_n) begin
  241. mem[0] <= 0;
  242. for (i=1; i<word_depth; i=i+1) begin
  243. case(i)
  244. 32'd2: mem[i] <= 32'hbffffff0;
  245. 32'd3: mem[i] <= 32'h10008000;
  246. default: mem[i] <= 32'h0;
  247. endcase
  248. end
  249. end
  250. else begin
  251. mem[0] <= 0;
  252. for (i=1; i<word_depth; i=i+1)
  253. mem[i] <= mem_nxt[i];
  254. end
  255. end
  256. endmodule
  257.  
  258. module MULDIV_unit(
  259. // TODO: port declaration
  260. );
  261. // Todo: HW2
  262.  
  263. endmodule
  264.  
  265. module controller(inst, func3, func7,
  266. branch, mem_write, mem_load, rs1_pc, rs2_imm, ALUop,
  267. MULop, jump, jal_register, reg_write);
  268.  
  269. parameter ALU = 'b0110011 ;
  270. parameter ALUI = 'b0010011 ;
  271. parameter LOAD = 'b0000011 ;
  272. parameter SAVE = 'b0100011 ;
  273. parameter AUIPC = 'b0010111 ;
  274. parameter JAL = 'b1101111;
  275. parameter JALR = 'b1100111 ;
  276. parameter BRANCH = 'b1100011 ;
  277.  
  278.  
  279. parameter ADD_FUNC3 = 'b000 ;
  280. parameter SUB_FUNC3 = 'b000 ;
  281. parameter AND_FUNC3 = 'b111 ;
  282. parameter XOR_FUNC3 = 'b100 ;
  283. parameter MUL_FUNC3 = 'b000 ;
  284.  
  285. parameter ADD_FUNC7 = 'b0000000 ;
  286. parameter SUB_FUNC7 = 'b0100000 ;
  287. parameter MUL_FUNC7 = 'b0000001 ;
  288.  
  289. parameter ADDI_FUNC3 = 'b000 ;
  290. parameter SLLI_FUNC3 = 'b001 ;
  291. parameter SLTI_FUNC3 = 'b010 ;
  292. parameter SRAI_FUNC3 = 'b101 ;
  293.  
  294. parameter BEQ_FUNC3 = 'b000;
  295. parameter BNE_FUNC3 = 'b001;
  296. parameter BLT_FUNC3 = 'b100;
  297. parameter BGE_FUNC3 = 'b101;
  298.  
  299. parameter ADD = 0 ;
  300. parameter SUB = 1 ;
  301. parameter AND = 2 ;
  302. parameter XOR = 3 ;
  303. parameter SLL = 4 ;
  304. parameter SLT = 5 ;
  305. parameter SRA = 6 ;
  306.  
  307.  
  308. input [6:0] inst ;
  309. input [2:0] func3 ;
  310. input [6:0] func7 ;
  311. output reg branch ; //high if branched
  312. output reg mem_write ;
  313. output reg mem_load ;
  314. //output mem_reg ;
  315. output reg rs1_pc ; //1 if from pc
  316. output reg rs2_imm ; //low if from register
  317. output reg [2:0] ALUop ;
  318. output reg MULop ;
  319. output reg jump ;
  320. output reg jal_register ;
  321. output reg reg_write ;
  322.  
  323. // ------ ------
  324. always @(*) begin
  325. case (inst)
  326. ALU: begin
  327. branch = 0 ;
  328. mem_load = 0 ;
  329. mem_write = 0 ;
  330. rs1_pc = 0;
  331. rs2_imm = 0 ;
  332. jump = 0 ;
  333. jal_register = 0 ;
  334. reg_write = 1 ;
  335. case (func3)
  336. ADD_FUNC3, SUB_FUNC3 : begin
  337. if (func7 == ADD_FUNC7) begin
  338. ALUop = ADD ;
  339. MULop = 0 ;
  340. end
  341. else if (func7 == SUB_FUNC7) begin
  342. ALUop = SUB ;
  343. MULop = 0 ;
  344. end
  345. else begin //func7 == MUL_FUNC7
  346. ALUop = 0 ;
  347. MULop = 1 ;
  348. end
  349. end
  350. AND_FUNC3 : begin
  351. ALUop = AND ;
  352. MULop = 0 ;
  353. end
  354. XOR_FUNC3 : begin
  355. ALUop = XOR ;
  356. MULop = 0 ;
  357. end
  358. default : begin
  359. ALUop = ALUop ;
  360. MULop = MULop ;
  361. end
  362. endcase
  363. end
  364. ALUI: begin
  365. branch = 0 ;
  366. mem_load = 0 ;
  367. mem_write = 0 ;
  368. rs1_pc = 0 ;
  369. rs2_imm = 1 ;
  370. MULop = 0 ;
  371. jump = 0 ;
  372. jal_register = 0 ;
  373. reg_write = 1 ;
  374. case(func7)
  375. ADDI_FUNC3: begin
  376. ALUop = ADD ;
  377. end
  378. SLLI_FUNC3: begin
  379. ALUop = SLL ;
  380. end
  381. SLTI_FUNC3: begin
  382. ALUop = SLT ;
  383. end
  384. SRAI_FUNC3: begin
  385. ALUop = SRA ;
  386. end
  387. default: begin
  388. ALUop = ALUop ;
  389. end
  390. endcase
  391. end
  392. AUIPC: begin
  393. branch = 0 ;
  394. mem_load = 0 ;
  395. mem_write = 0 ;
  396. rs1_pc = 0 ;
  397. rs2_imm = 1 ;
  398. MULop = 0 ;
  399. ALUop = 0 ;
  400. reg_write = 1 ;
  401. jump = 0 ;
  402. jal_register = 0 ;
  403. end
  404. JAL: begin
  405. branch = 0 ;
  406. mem_load = 0 ;
  407. mem_write = 0 ;
  408. rs1_pc = 1 ;
  409. rs2_imm = 1 ;
  410. MULop = 0 ;
  411. ALUop = 0 ;
  412. reg_write = 1 ;
  413. jump = 1 ;
  414. jal_register = 0 ;
  415. end
  416. JALR: begin
  417. branch = 0 ;
  418. mem_load = 0 ;
  419. mem_write = 0 ;
  420. rs1_pc = 0 ;
  421. rs2_imm = 1 ;
  422. MULop = 0 ;
  423. ALUop = ADD ;
  424. reg_write = 1 ;
  425. jump = 0 ;
  426. jal_register = 1 ;
  427. end
  428. LOAD: begin
  429. branch = 0 ;
  430. mem_load = 1 ;
  431. mem_write = 0 ;
  432. rs1_pc = 0 ;
  433. rs2_imm = 0 ;
  434. MULop = 0 ;
  435. ALUop = 0 ;
  436. reg_write = 1 ;
  437. jump = 0 ;
  438. jal_register = 0 ;
  439. end
  440. SAVE: begin
  441. branch = 0 ;
  442. mem_load = 0 ;
  443. mem_write = 1 ;
  444. rs1_pc = 0 ;
  445. rs2_imm = 0 ;
  446. MULop = 0 ;
  447. ALUop = 0 ;
  448. reg_write = 0 ;
  449. jump = 0 ;
  450. jal_register = 0 ;
  451. end
  452. BRANCH: begin
  453. branch = 1 ;
  454. mem_load = 0 ;
  455. mem_write = 0 ;
  456. rs1_pc = 0 ;
  457. rs2_imm = 0 ;
  458. MULop = 0 ;
  459. ALUop = 0 ;
  460. reg_write = 0 ;
  461. jump = 0 ;
  462. jal_register = 0 ;
  463. end
  464. endcase
  465. end
  466. endmodule
  467.  
  468. module Cache#(
  469. parameter BIT_W = 32,
  470. parameter ADDR_W = 32
  471. )(
  472. input i_clk,
  473. input i_rst_n,
  474. // processor interface
  475. input i_proc_cen,
  476. input i_proc_wen,
  477. input [ADDR_W-1:0] i_proc_addr,
  478. input [BIT_W-1:0] i_proc_wdata,
  479. output [BIT_W-1:0] o_proc_rdata,
  480. output o_proc_stall,
  481. input i_proc_finish,
  482. output o_cache_finish,
  483. // memory interface
  484. output o_mem_cen,
  485. output o_mem_wen,
  486. output [ADDR_W-1:0] o_mem_addr,
  487. output [BIT_W*4-1:0] o_mem_wdata,
  488. input [BIT_W*4-1:0] i_mem_rdata,
  489. input i_mem_stall,
  490. output o_cache_available,
  491. // others
  492. input [ADDR_W-1: 0] i_offset
  493. );
  494.  
  495. assign o_cache_available = 0; // change this value to 1 if the cache is implemented
  496.  
  497. //------------------------------------------//
  498. // default connection //
  499. assign o_mem_cen = i_proc_cen; //
  500. assign o_mem_wen = i_proc_wen; //
  501. assign o_mem_addr = i_proc_addr; //
  502. assign o_mem_wdata = i_proc_wdata; //
  503. assign o_proc_rdata = i_mem_rdata[0+:BIT_W];//
  504. assign o_proc_stall = i_mem_stall; //
  505. //------------------------------------------//
  506.  
  507. // Todo: BONUS
  508.  
  509. endmodule
Advertisement
Add Comment
Please, Sign In to add comment