This repository has been archived on 2023-12-21. You can view files and clone it, but cannot push or open issues or pull requests.
cse141L-project/RTL/InstROM.sv

57 lines
1.9 KiB
Systemverilog
Raw Normal View History

2022-08-12 05:01:28 +00:00
// Create Date: 15:50:22 10/02/2019
// Design Name:
// Module Name: InstROM
// Project Name: CSE141L
// Tool versions:
// Description: Verilog module -- instruction ROM template
// preprogrammed with instruction values (see case statement)
//
2022-08-13 22:34:01 +00:00
// Revision: 2021.08.08
2022-08-14 01:54:10 +00:00
//
// A = program counter width
2022-08-13 22:34:01 +00:00
// W = machine code width -- do not change for CSE141L
module InstROM #(parameter A=10, W=9) (
2022-08-12 05:01:28 +00:00
input [A-1:0] InstAddress,
output logic[W-1:0] InstOut);
2022-08-14 01:54:10 +00:00
// (usually recommended) expression
// need $readmemh or $readmemb to initialize all of the elements
// This version will work best with assemblers, but you can try the alternative starting line 33
// This version is also by far the easiest if you have a long program scrip.
// declare 2-dimensional array, W bits wide, 2**A words deep
logic[W-1:0] inst_rom[2**A];
always_comb InstOut = inst_rom[InstAddress];
initial begin // load from external text file
$readmemb("machine_code.txt",inst_rom);
end
2022-08-12 05:01:28 +00:00
2022-08-14 01:54:10 +00:00
// Sample instruction format:
2022-08-12 05:01:28 +00:00
// {3bit opcode, 3bit rs or rt, 3bit rt, immediate, or branch target}
// then use LUT to map 3 bits to 10 for branch target, 8 for immediate
2022-08-14 01:54:10 +00:00
/* alternative to code shown below, which may be simpler -- either is fine
2022-08-12 05:01:28 +00:00
always_comb begin
InstOut = 'b0000000000; // default
case (InstAddress)
//opcode = 0 lhw, rs = 0, rt = 1
0 : InstOut = 'b0000000001; // load from address at reg 0 to reg 1
// opcode = 1 addi, rs/rt = 1, immediate = 1
1 : InstOut = 'b0001001001; // addi reg 1 and 1
// opcode = 2 shw, rs = 0, rt = 1
2 : InstOut = 'b0010000001; // sw reg 1 to address in reg 0
// opcode = 3 beqz, rs = 1, target = 1
3 : InstOut = 'b0011001001; // beqz reg1 to absolute address 1
// opcode = 15 halt
4 : InstOut = '1; // equiv to 10'b1111111111 or 'b1111111111 halt
2022-08-14 01:54:10 +00:00
// (default case already covered by opening statement)
endcase
end
*/
2022-08-12 05:01:28 +00:00
endmodule