1
1
`timescale 1ns / 1ps
2
2
3
- // module to read instructions from rom and control program counter jumps
3
+ // module to read/write instructions from ram and control program counter jumps
4
4
5
- module instr_rom (
5
+ module instr_ram (
6
6
// synchronize with posedge of clk
7
7
input clk,
8
8
// only update values when clk_en is high
@@ -15,17 +15,26 @@ module instr_rom(
15
15
input signed [10 :0 ] acc,
16
16
// address to jump to if jump is performed
17
17
input signed [10 :0 ] jmp_off,
18
+ // controls whether to write a new instruction
19
+ input write_en,
20
+ // address to write new instruction
21
+ input [4 :0 ] write_addr,
22
+ // new instruction to write.
23
+ // Writes must end with highest address instruction.
24
+ input [20 :0 ] write_data,
18
25
// instruction at current program counter
19
26
output [20 :0 ] opcode
20
27
);
21
28
`include "my_params.vh"
22
- // memory file to initialize ROM with
29
+ // memory file to initialize RAM with
23
30
parameter MEM_INIT_FILE = "" ;
24
- // number of valid instructions in ROM
31
+ // number of valid instructions in MEM_INIT_FILE
25
32
parameter NUM_ENTRIES = 5'd10 ;
26
33
27
34
// memory for storing instructinos
28
35
reg [20 :0 ] ram[31 :0 ];
36
+ // number of valid instructions in RAM
37
+ reg [4 :0 ] num_entries;
29
38
30
39
// program counter pointing to current instruction
31
40
reg [4 :0 ] pc;
@@ -51,6 +60,7 @@ module instr_rom(
51
60
initial begin
52
61
if (MEM_INIT_FILE != "" ) begin
53
62
$readmemb (MEM_INIT_FILE, ram);
63
+ num_entries = NUM_ENTRIES;
54
64
end
55
65
end
56
66
@@ -68,9 +78,9 @@ module instr_rom(
68
78
begin
69
79
pc <= 0 ;
70
80
end
71
- else if (pc_jmp >= NUM_ENTRIES )
81
+ else if (pc_jmp >= num_entries )
72
82
begin
73
- pc <= NUM_ENTRIES - 1 ;
83
+ pc <= num_entries - 1 ;
74
84
end
75
85
else
76
86
begin
@@ -79,7 +89,7 @@ module instr_rom(
79
89
end
80
90
else
81
91
begin
82
- if (pc == NUM_ENTRIES - 1 )
92
+ if (pc == num_entries - 1 )
83
93
begin
84
94
pc <= 0 ;
85
95
end
@@ -89,6 +99,11 @@ module instr_rom(
89
99
end
90
100
end
91
101
end
102
+ if (write_en)
103
+ begin
104
+ ram[write_addr] <= write_data;
105
+ num_entries <= write_addr + 5'd1 ;
106
+ end
92
107
end
93
108
94
109
endmodule
0 commit comments