This repository has been archived by the owner on Jun 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmem.v
135 lines (115 loc) · 3.76 KB
/
mem.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
`include "uart-tx.v"
module ram(
input rst,
input clk,
input write_enable,
input [ADDRESS_WIDTH-1 : 0] read_addr,
input [ADDRESS_WIDTH-1 : 0] write_addr,
input [WIDTH-1 : 0] data_in,
input [2:0] store_funct3,
output [WIDTH-1 : 0] data_out,
output uart_tx_wire);
parameter ADDRESS_WIDTH = 12;
parameter WIDTH = 32;
parameter IO_SPACE_WIDTH = 2; //MSB bits for memory mapped IO
parameter SB_FUNCT3 = 3'b000;
parameter SH_FUNCT3 = 3'b001;
parameter SW_FUNCT3 = 3'b010;
localparam MEMORY_SIZE = 1 << (ADDRESS_WIDTH - IO_SPACE_WIDTH - 1);
localparam WORD_ALIGNMENT = $clog2(WIDTH / 8);
localparam ALIGNED_WIDTH = ADDRESS_WIDTH - WORD_ALIGNMENT;
localparam IO_START = MEMORY_SIZE << IO_SPACE_WIDTH;
localparam UART_OFFSET = 4;
// UART TX IO mapping
// [31 : 16] unused, [15:8] tx data, [7 : 2] unused [1] uart tx ready flag, [0] uart tx start
localparam UART_BASE = IO_START + UART_OFFSET;
reg [WIDTH-1 : 0] mem [0 : MEMORY_SIZE-1];
wire [ALIGNED_WIDTH-1 : 0] read_addr_aligned, write_addr_aligned;
reg uart_start;
reg [7 : 0] uart_tx_buffer;
wire uart_ready;
initial begin
`ifdef IVERILOG
$readmemh(`TEST_PROG, mem);
`else
$readmemh("firmware.hex", mem);
`endif
end
uarttx tx(
.rst(rst),
.clk(clk),
.tx_start(uart_start),
.tx_byte(uart_tx_buffer),
.tx(uart_tx_wire),
.tx_ready(uart_ready));
assign read_addr_aligned = read_addr[ADDRESS_WIDTH-1 : WORD_ALIGNMENT];
assign write_addr_aligned = write_addr[ADDRESS_WIDTH-1 : WORD_ALIGNMENT];
assign data_out = (read_addr == UART_BASE) ?
{16'b0, uart_tx_buffer, 6'b0, uart_ready, uart_start} : mem[read_addr_aligned];
always @(posedge clk) begin
if (rst) begin
uart_start <= 0;
end else begin
if (write_enable) begin
if (write_addr == UART_BASE) begin
uart_start <= data_in[0];
uart_tx_buffer <= data_in[15 : 8];
end else begin
case (store_funct3)
SW_FUNCT3: begin
mem[write_addr_aligned] <= data_in;
end
SB_FUNCT3: begin
case (write_addr[1:0])
2'b00: begin
mem[write_addr_aligned] [7:0] <= data_in[7:0];
end
2'b01: begin
mem[write_addr_aligned] [15:8] <= data_in[7:0];
end
2'b10: begin
mem[write_addr_aligned] [23:16] <= data_in[7:0];
end
2'b11: begin
mem[write_addr_aligned] [31:24] <= data_in[7:0];
end
endcase
end
SH_FUNCT3: begin
if (write_addr[1]) begin
mem[write_addr_aligned] [31:16] <= data_in[15:0];
end else begin
mem[write_addr_aligned] [15:0] <= data_in[15:0];
end
if (write_addr[0]) begin
`ifdef IVERILOG
$display("Unaligned word write!\n");
`endif
end
end
default: begin
`ifdef IVERILOG
$display("Unsupported store funct3: %d!\n", store_funct3);
`endif
end
endcase
end
end
end
end
endmodule