-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmemory_manager.sv
94 lines (73 loc) · 2.73 KB
/
memory_manager.sv
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
module MemoryManager (
input clock,
input [16:0] videoAddress,
input [16:0] videoAddressOffset,
output logic [7:0] videoData,
output logic videoDataReady,
input memoryWriteRequest,
input [16:0] memoryWriteAddress,
input [7:0] memoryWriteData,
output logic memoryWriteComplete,
input [16:0] memoryReadAddress,
output logic [7:0] memoryReadData,
output logic [16:0] ramAddress,
inout [7:0] ramData,
output logic ramWriteEnable
);
parameter
STATE_IDLE = 0,
STATE_VIDEO_READ = 1,
STATE_MEM_READ = 2,
STATE_MEM_WRITE = 3,
STATE_COMPLETE = 4;
logic [2:0] currentState;
logic [2:0] nextState;
logic memoryWriteStarted;
always_comb begin
case (currentState)
STATE_IDLE: nextState = STATE_VIDEO_READ;
STATE_VIDEO_READ: if (memoryWriteRequest) nextState = STATE_MEM_WRITE;
else nextState = STATE_MEM_READ;
STATE_MEM_WRITE: nextState = STATE_COMPLETE;
STATE_MEM_READ: nextState = STATE_COMPLETE;
STATE_COMPLETE: nextState = STATE_IDLE;
default: nextState = STATE_IDLE;
endcase
end
always_ff @(posedge clock) begin
if (memoryWriteRequest && (nextState == STATE_MEM_WRITE)) begin
memoryWriteStarted <= 1;
end
if (memoryWriteStarted && (nextState == STATE_COMPLETE)) begin
memoryWriteComplete <= 1;
memoryWriteStarted <= 0;
end else begin
memoryWriteComplete <= 0;
end
end
always_ff @(posedge clock) begin
currentState <= nextState;
end
always_ff @(negedge clock) begin
case(nextState)
STATE_IDLE,
STATE_VIDEO_READ : ramAddress <= videoAddress + videoAddressOffset;
STATE_MEM_READ : ramAddress <= memoryReadAddress + videoAddressOffset;
STATE_MEM_WRITE : ramAddress <= memoryWriteAddress + videoAddressOffset;
endcase
end
assign ramData = (memoryWriteStarted || memoryWriteComplete) ? memoryWriteData : 8'bZ;
always_ff @(posedge clock) begin
ramWriteEnable <= ~(nextState == STATE_MEM_WRITE);
end
always_ff @(posedge clock) begin
case (currentState)
STATE_VIDEO_READ : videoData <= ramData;
STATE_MEM_READ : memoryReadData <= ramData;
endcase
end
always_ff @(posedge clock) begin
videoDataReady <= (currentState == STATE_MEM_READ ||
currentState == STATE_MEM_WRITE);
end
endmodule