Skip to content

Commit 551ecbb

Browse files
committed
added check for memory out of bounds
1 parent 24d465d commit 551ecbb

File tree

7 files changed

+1090
-302699
lines changed

7 files changed

+1090
-302699
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ chmod +x test_sequential.sh
1212
./test_sequential.sh <filename>.s
1313
```
1414

15-
PS: I recommend testing 2.s ( a really long programme that checks all the ALU funciton) and 5.s ( has all the instructions.)
15+
PS: I recommend testing 2.s ( a really long programme that checks all the ALU funciton) and 5.s ( has all the instructions). 6.s checks for invalid data memory address.
1616
```bash
1717
./test_sequential.sh 5.s
1818
./test_sequential.sh 2.s
19+
./test_sequential.sh 6.s
1920
```
2021

2122
## Assembly Instructions Supported
@@ -31,7 +32,7 @@ The assembler supports a subset of RISC-V instructions:
3132
- `ld` and `sd` are used to load and store double words. But, they use the same instructions as `lw` and `sw` that are not implemented in this version.
3233
- Since data memory only supports 64 bit read and write, remember to only use addresses that are multiples of 8.
3334

34-
## Web visulization:
35+
## Web visualization:
3536
- if doesn't run by doing the test_sequential.sh otherwise it should just open a web browser
3637
```bash
3738
cd sequential

sequential/modules/data_memory.v

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ module data_memory(
1515
end
1616

1717
always @(posedge clk) begin
18-
if (mem_write) begin
18+
if (address > 1016) begin
19+
$fatal(1, "\n\nError: Invalid memory address %d\n", address);
20+
end
21+
if (mem_write && address <= 1016) begin
1922
memory[address] <= write_data[7:0];
2023
memory[address+1] <= write_data[15:8];
2124
memory[address+2] <= write_data[23:16];
@@ -28,7 +31,10 @@ module data_memory(
2831
end
2932

3033
always @(*) begin
31-
if (mem_read) begin
34+
if (address > 1016) begin
35+
$fatal(1, "\n\nError: Invalid memory address %d\n", address);
36+
end
37+
if (mem_read && address <= 1016) begin
3238
read_data = {memory[address+7], memory[address+6], memory[address+5], memory[address+4],
3339
memory[address+3], memory[address+2], memory[address+1], memory[address]};
3440
end else begin
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
module instruction_memory(
22
input [63:0] pc,
3-
output [31:0] instruction
3+
output reg [31:0] instruction
44
);
55
reg [31:0] memory [0:1023]; // 1024 max instructions
66

7-
assign instruction = memory[pc[11:2]]; // 10 bits to address 1024 instructions
7+
always @(*) begin
8+
if (pc[11:2] > 1023) begin
9+
$fatal(1, "Error: Invalid instruction address %d (out of range)", pc);
10+
end else begin
11+
instruction = memory[pc[11:2]]; // Access instruction memory (byte-aligned)
12+
end
13+
end
14+
815
endmodule

sequential/testcases/6.s

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
begin:
2+
addi x6, x0, 10
3+
4+
addi x7, x0, 0
5+
addi x8, x0, 1024
6+
7+
sd x6 0(x7)
8+
sd x6 0(x8)
9+
10+
end:
11+
nop

0 commit comments

Comments
 (0)