-
Notifications
You must be signed in to change notification settings - Fork 8
/
turbo9_microsequencer.v
185 lines (158 loc) · 6.07 KB
/
turbo9_microsequencer.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// [TURBO9_HEADER_START]
//////////////////////////////////////////////////////////////////////////////
// Turbo9 Microprocessor IP
//////////////////////////////////////////////////////////////////////////////
// Website: www.turbo9.org
// Contact: team[at]turbo9[dot]org
//////////////////////////////////////////////////////////////////////////////
// [TURBO9_LICENSE_START]
// BSD-1-Clause
//
// Copyright (c) 2020-2023
// Kevin Phillipson
// Michael Rywalt
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
// [TURBO9_LICENSE_END]
//////////////////////////////////////////////////////////////////////////////
// Engineer: Kevin Phillipson
// Description: Turbo9 microsequencer
//
//////////////////////////////////////////////////////////////////////////////
// History:
// 07.14.2023 - Kevin Phillipson
// File header added
//
//////////////////////////////////////////////////////////////////////////////
// [TURBO9_HEADER_END]
/////////////////////////////////////////////////////////////////////////////
// MODULE
/////////////////////////////////////////////////////////////////////////////
module turbo9_microsequencer
(
// Inputs: Clock & Reset
input RST_I,
input CLK_I,
// Inputs
input STALL_MICROCYCLE_I,
input CONDITION_I,
input [2:0] MICRO_SEQ_OP_I,
input [7:0] MICRO_SEQ_BRANCH_ADDR_I,
input [7:0] JUMP_TABLE_A_I,
input [7:0] JUMP_TABLE_B_I,
// Outputs
output MICROCYCLE_START_O,
output [7:0] MICROCODE_ADR_O,
output EXECUTE_NEXT_INSTR_O
);
/////////////////////////////////////////////////////////////////////////////
// INTERNAL SIGNALS
/////////////////////////////////////////////////////////////////////////////
reg [7:0] micro_pc_reg;
reg [7:0] micro_pc_nxt;
wire [7:0] micro_pc_inc;
localparam micro_pc_rst = 8'h00;
reg return_load;
reg [7:0] return_reg;
localparam return_rst = 8'h00;
reg microcycle_start_reg;
localparam microcycle_start_rst = 1'b1;
// Note this encoding is critical.
// OP_JUMP_TABLE_A_NEXT_PC is partially decoded for speed
localparam OP_CONTINUE = 3'b000;
localparam OP_JUMP = 3'b001;
localparam OP_CALL = 3'b010;
localparam OP_RETURN = 3'b011;
localparam OP_JUMP_TABLE_B = 3'b100;
localparam OP_JUMP_TABLE_A_NEXT_PC = 3'b1?1;
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// NEXT STATE LOGIC
/////////////////////////////////////////////////////////////////////////////
assign micro_pc_inc = micro_pc_reg + 8'h01;
always @* begin
// Default State
micro_pc_nxt = micro_pc_rst;
return_load = 1'b0;
// Next State Logic
//unique casez ({MICRO_SEQ_OP_I, CONDITION_I}) // FIXME optimize logic
casez ({MICRO_SEQ_OP_I, CONDITION_I})
////////////////////////////////////////// Conditional Cases
{OP_CONTINUE, 1'b1}: begin
micro_pc_nxt = micro_pc_inc;
end
//
{OP_JUMP, 1'b1}: begin
micro_pc_nxt = MICRO_SEQ_BRANCH_ADDR_I;
end
//
{OP_CALL, 1'b1}: begin
return_load = 1'b1;
micro_pc_nxt = MICRO_SEQ_BRANCH_ADDR_I;
end
//
{OP_RETURN, 1'b1}: begin
micro_pc_nxt = return_reg;
end
//
{OP_JUMP_TABLE_B, 1'b1}: begin
micro_pc_nxt = JUMP_TABLE_B_I;
end
////////////////////////////////////////// Unconditional Cases
{OP_JUMP_TABLE_A_NEXT_PC, 1'b?}: begin
micro_pc_nxt = JUMP_TABLE_A_I;
end
//
default: begin // CONTINUE
micro_pc_nxt = micro_pc_inc;
end
endcase
end
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// REGISTERS
/////////////////////////////////////////////////////////////////////////////
always @(posedge CLK_I, posedge RST_I) begin
if (RST_I) begin
micro_pc_reg <= micro_pc_rst;
return_reg <= return_rst;
microcycle_start_reg <= microcycle_start_rst;
end else begin
if (~STALL_MICROCYCLE_I) begin
micro_pc_reg <= micro_pc_nxt;
//
if (return_load)
return_reg <= micro_pc_inc;
//
microcycle_start_reg <= 1'b1;
end else begin
microcycle_start_reg <= 1'b0;
end
end
end
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// ASSIGN OUTPUTS
/////////////////////////////////////////////////////////////////////////////
assign MICROCYCLE_START_O = microcycle_start_reg;
assign MICROCODE_ADR_O = STALL_MICROCYCLE_I ? micro_pc_reg : micro_pc_nxt;
assign EXECUTE_NEXT_INSTR_O = MICRO_SEQ_OP_I[2] & MICRO_SEQ_OP_I[0]; // OP_JUMP_TABLE_A_NEXT_PC
/////////////////////////////////////////////////////////////////////////////
endmodule