1 |
16 |
csantifort |
//////////////////////////////////////////////////////////////////
|
2 |
|
|
// //
|
3 |
|
|
// Functions for Amber 25 Core //
|
4 |
|
|
// //
|
5 |
|
|
// This file is part of the Amber project //
|
6 |
|
|
// http://www.opencores.org/project,amber //
|
7 |
|
|
// //
|
8 |
|
|
// Description //
|
9 |
|
|
// Functions used in more than one module //
|
10 |
|
|
// //
|
11 |
|
|
// Author(s): //
|
12 |
|
|
// - Conor Santifort, csantifort.amber@gmail.com //
|
13 |
|
|
// //
|
14 |
|
|
//////////////////////////////////////////////////////////////////
|
15 |
|
|
// //
|
16 |
|
|
// Copyright (C) 2011 Authors and OPENCORES.ORG //
|
17 |
|
|
// //
|
18 |
|
|
// This source file may be used and distributed without //
|
19 |
|
|
// restriction provided that this copyright statement is not //
|
20 |
|
|
// removed from the file and that any derivative work contains //
|
21 |
|
|
// the original copyright notice and the associated disclaimer. //
|
22 |
|
|
// //
|
23 |
|
|
// This source file is free software; you can redistribute it //
|
24 |
|
|
// and/or modify it under the terms of the GNU Lesser General //
|
25 |
|
|
// Public License as published by the Free Software Foundation; //
|
26 |
|
|
// either version 2.1 of the License, or (at your option) any //
|
27 |
|
|
// later version. //
|
28 |
|
|
// //
|
29 |
|
|
// This source is distributed in the hope that it will be //
|
30 |
|
|
// useful, but WITHOUT ANY WARRANTY; without even the implied //
|
31 |
|
|
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
|
32 |
|
|
// PURPOSE. See the GNU Lesser General Public License for more //
|
33 |
|
|
// details. //
|
34 |
|
|
// //
|
35 |
|
|
// You should have received a copy of the GNU Lesser General //
|
36 |
|
|
// Public License along with this source; if not, download it //
|
37 |
|
|
// from http://www.opencores.org/lgpl.shtml //
|
38 |
|
|
// //
|
39 |
|
|
//////////////////////////////////////////////////////////////////
|
40 |
|
|
|
41 |
|
|
|
42 |
|
|
// ========================================================
|
43 |
35 |
csantifort |
// Instruction type decode
|
44 |
|
|
// ========================================================
|
45 |
|
|
function [3:0] instruction_type;
|
46 |
|
|
input [31:0] instruction;
|
47 |
|
|
begin
|
48 |
|
|
// Instruction Decode - Order is important!
|
49 |
|
|
casez ({instruction[27:20], instruction[7:4]})
|
50 |
|
|
12'b00010?001001 : instruction_type = SWAP;
|
51 |
|
|
12'b000000??1001 : instruction_type = MULT;
|
52 |
|
|
12'b00?????????? : instruction_type = REGOP;
|
53 |
|
|
12'b01?????????? : instruction_type = TRANS;
|
54 |
|
|
12'b100????????? : instruction_type = MTRANS;
|
55 |
|
|
12'b101????????? : instruction_type = BRANCH;
|
56 |
|
|
12'b110????????? : instruction_type = CODTRANS;
|
57 |
|
|
12'b1110???????0 : instruction_type = COREGOP;
|
58 |
|
|
12'b1110???????1 : instruction_type = CORTRANS;
|
59 |
|
|
default: instruction_type = SWI;
|
60 |
|
|
endcase
|
61 |
|
|
end
|
62 |
|
|
endfunction
|
63 |
|
|
|
64 |
|
|
|
65 |
|
|
// ========================================================
|
66 |
|
|
// Select 32 bits from a 128 bit bus based on a 2-bit address
|
67 |
|
|
// ========================================================
|
68 |
|
|
function [31:0] sel32_128;
|
69 |
|
|
input [1:0] select;
|
70 |
|
|
input [127:0] bus;
|
71 |
|
|
begin
|
72 |
|
|
sel32_128 = select==2'd0 ? bus[31:0] : select==2'd1 ? bus[63:32] : select==2'd2 ? bus[95:64] : bus[127:96];
|
73 |
|
|
end
|
74 |
|
|
endfunction
|
75 |
|
|
|
76 |
|
|
|
77 |
|
|
// ========================================================
|
78 |
16 |
csantifort |
// PC Filter - Remove the status bits
|
79 |
|
|
// ========================================================
|
80 |
|
|
function [31:0] pcf;
|
81 |
|
|
input [31:0] pc_reg;
|
82 |
|
|
begin
|
83 |
|
|
pcf = {6'd0, pc_reg[25:2], 2'd0};
|
84 |
|
|
end
|
85 |
|
|
endfunction
|
86 |
|
|
|
87 |
|
|
|
88 |
|
|
// ========================================================
|
89 |
|
|
// 4-bit to 16-bit 1-hot decode
|
90 |
|
|
// ========================================================
|
91 |
|
|
function [14:0] decode;
|
92 |
|
|
input [3:0] reg_sel;
|
93 |
|
|
begin
|
94 |
|
|
case ( reg_sel )
|
95 |
|
|
4'h0: decode = 15'h0001;
|
96 |
|
|
4'h1: decode = 15'h0002;
|
97 |
|
|
4'h2: decode = 15'h0004;
|
98 |
|
|
4'h3: decode = 15'h0008;
|
99 |
|
|
4'h4: decode = 15'h0010;
|
100 |
|
|
4'h5: decode = 15'h0020;
|
101 |
|
|
4'h6: decode = 15'h0040;
|
102 |
|
|
4'h7: decode = 15'h0080;
|
103 |
|
|
4'h8: decode = 15'h0100;
|
104 |
|
|
4'h9: decode = 15'h0200;
|
105 |
|
|
4'ha: decode = 15'h0400;
|
106 |
|
|
4'hb: decode = 15'h0800;
|
107 |
|
|
4'hc: decode = 15'h1000;
|
108 |
|
|
4'hd: decode = 15'h2000;
|
109 |
|
|
4'he: decode = 15'h4000;
|
110 |
|
|
default: decode = 15'h0000;
|
111 |
|
|
endcase
|
112 |
|
|
end
|
113 |
|
|
endfunction
|
114 |
|
|
|
115 |
|
|
|
116 |
|
|
// ========================================================
|
117 |
|
|
// Convert Stats Bits Mode to one-hot encoded version
|
118 |
|
|
// ========================================================
|
119 |
|
|
function [3:0] oh_status_bits_mode;
|
120 |
|
|
input [1:0] fn_status_bits_mode;
|
121 |
|
|
begin
|
122 |
|
|
oh_status_bits_mode =
|
123 |
|
|
fn_status_bits_mode == SVC ? 1'd1 << OH_SVC :
|
124 |
|
|
fn_status_bits_mode == IRQ ? 1'd1 << OH_IRQ :
|
125 |
|
|
fn_status_bits_mode == FIRQ ? 1'd1 << OH_FIRQ :
|
126 |
|
|
1'd1 << OH_USR ;
|
127 |
|
|
end
|
128 |
|
|
endfunction
|
129 |
|
|
|
130 |
|
|
// ========================================================
|
131 |
|
|
// Convert mode into ascii name
|
132 |
|
|
// ========================================================
|
133 |
|
|
function [(14*8)-1:0] mode_name;
|
134 |
|
|
input [4:0] mode;
|
135 |
|
|
begin
|
136 |
|
|
|
137 |
|
|
mode_name = mode == USR ? "User " :
|
138 |
|
|
mode == SVC ? "Supervisor " :
|
139 |
|
|
mode == IRQ ? "Interrupt " :
|
140 |
|
|
mode == FIRQ ? "Fast Interrupt" :
|
141 |
|
|
"UNKNOWN " ;
|
142 |
|
|
end
|
143 |
|
|
endfunction
|
144 |
|
|
|
145 |
|
|
|
146 |
|
|
// ========================================================
|
147 |
|
|
// Conditional Execution Function
|
148 |
|
|
// ========================================================
|
149 |
|
|
// EQ Z set
|
150 |
|
|
// NE Z clear
|
151 |
|
|
// CS C set
|
152 |
|
|
// CC C clear
|
153 |
|
|
// MI N set
|
154 |
|
|
// PL N clear
|
155 |
|
|
// VS V set
|
156 |
|
|
// VC V clear
|
157 |
|
|
// HI C set and Z clear
|
158 |
|
|
// LS C clear or Z set
|
159 |
|
|
// GE N == V
|
160 |
|
|
// LT N != V
|
161 |
|
|
// GT Z == 0,N == V
|
162 |
|
|
// LE Z == 1 or N != V
|
163 |
|
|
// AL Always (unconditional)
|
164 |
|
|
// NV Never
|
165 |
|
|
|
166 |
|
|
function conditional_execute;
|
167 |
|
|
input [3:0] condition;
|
168 |
|
|
input [3:0] flags;
|
169 |
|
|
begin
|
170 |
|
|
conditional_execute
|
171 |
|
|
= ( condition == AL ) ||
|
172 |
|
|
( condition == EQ && flags[2] ) ||
|
173 |
|
|
( condition == NE && !flags[2] ) ||
|
174 |
|
|
( condition == CS && flags[1] ) ||
|
175 |
|
|
( condition == CC && !flags[1] ) ||
|
176 |
|
|
( condition == MI && flags[3] ) ||
|
177 |
|
|
( condition == PL && !flags[3] ) ||
|
178 |
|
|
( condition == VS && flags[0] ) ||
|
179 |
|
|
( condition == VC && !flags[0] ) ||
|
180 |
|
|
|
181 |
|
|
( condition == HI && flags[1] && !flags[2] ) ||
|
182 |
|
|
( condition == LS && (!flags[1] || flags[2]) ) ||
|
183 |
|
|
|
184 |
|
|
( condition == GE && flags[3] == flags[0] ) ||
|
185 |
|
|
( condition == LT && flags[3] != flags[0] ) ||
|
186 |
|
|
|
187 |
|
|
( condition == GT && !flags[2] && flags[3] == flags[0] ) ||
|
188 |
|
|
( condition == LE && (flags[2] || flags[3] != flags[0])) ;
|
189 |
|
|
|
190 |
|
|
end
|
191 |
|
|
endfunction
|
192 |
|
|
|
193 |
|
|
|
194 |
|
|
// ========================================================
|
195 |
|
|
// Log 2
|
196 |
|
|
// ========================================================
|
197 |
|
|
|
198 |
|
|
function [31:0] log2;
|
199 |
|
|
input [31:0] num;
|
200 |
|
|
integer i;
|
201 |
|
|
|
202 |
|
|
begin
|
203 |
|
|
log2 = 32'd0;
|
204 |
|
|
for (i=0; i<30; i=i+1)
|
205 |
|
|
if ((2**i > num) && (log2 == 0))
|
206 |
|
|
log2 = i-1;
|
207 |
|
|
end
|
208 |
|
|
endfunction
|