-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmemory.v
executable file
·74 lines (54 loc) · 2.71 KB
/
memory.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//=========================================================
// Instruction memory
//=========================================================
`define EOF 32'hFFFF_FFFF
`define NULL 0
module Memory(inst_addr, instr, data_addr, data_in, mem_read, mem_write, data_out);
// Interface
input [4*8:1] inst_addr;
output [31:0] instr;
input [4*8:1] data_addr;
input [31:0] data_in;
input mem_read;
input mem_write;
output [31:0] data_out;
// Instruction memory is byte-addressable, instructions are word-aligned
// Instruction memory with 2k 8-bit words
// Instruction address range: 0x0000 ~ 0x03FC
parameter MEM_SIZE=32'h00002000;
integer i;
integer file, r;
reg [7:0] memory [0:MEM_SIZE-1];
reg [31:0] data_addr_reg, inst_addr_reg;
reg [12*8:1] rest;
initial
begin : file_block
// for(i=0; i<2048; i=i+1) begin
// memory[i] = 8'b0;
// end
file = $fopen("matrix.hexdump","r");
if (file == `NULL)
disable file_block;
// while (!$feof(file))
for (i = 0; i < 6 ; i=i+1)
begin
r = $fscanf(file, "%h %h %h %h %h %h %h %h %h %h %h %h %h %h %h %h %h %s\n", data_addr_reg, memory[data_addr_reg], memory[data_addr_reg+1], memory[data_addr_reg+2], memory[data_addr_reg+3], memory[data_addr_reg+4], memory[data_addr_reg+5], memory[data_addr_reg+6], memory[data_addr_reg+7], memory[data_addr_reg+8], memory[data_addr_reg+9], memory[data_addr_reg+10], memory[data_addr_reg+11], memory[data_addr_reg+12], memory[data_addr_reg+13], memory[data_addr_reg+14], memory[data_addr_reg+15], rest);
end // while command
r = $fscanf(file, "%s\n", rest);
for (i = 0; i < 9 ; i=i+1)
begin
r = $fscanf(file, "%h %h %h %h %h %h %h %h %h %h %h %h %h %h %h %h %h %s\n", inst_addr_reg, memory[inst_addr_reg], memory[inst_addr_reg+1], memory[inst_addr_reg+2], memory[inst_addr_reg+3], memory[inst_addr_reg+4], memory[inst_addr_reg+5], memory[inst_addr_reg+6], memory[inst_addr_reg+7], memory[inst_addr_reg+8], memory[inst_addr_reg+9], memory[inst_addr_reg+10], memory[inst_addr_reg+11], memory[inst_addr_reg+12], memory[inst_addr_reg+13], memory[inst_addr_reg+14], memory[inst_addr_reg+15], rest);
end // while command
$fclose(file);
end // initial
assign data_out = (mem_read) ? {memory[data_addr+3], memory[data_addr+2], memory[data_addr+1], memory[data_addr]} : 32'b0;
always @ (posedge mem_write)
begin
memory[data_addr+3] <= data_in[31:24];
memory[data_addr+2] <= data_in[23:16];
memory[data_addr+1] <= data_in[15:8];
memory[data_addr] <= data_in[7:0];
end
assign instr = {memory[inst_addr+3], memory[inst_addr+2], memory[inst_addr+1], memory[inst_addr]} ;
// Read Data
endmodule