| 1 |
2 |
idiolatrie |
--------------------------------------------------------------------------------
|
| 2 |
|
|
-- MIPS™ I CPU - Functions --
|
| 3 |
|
|
--------------------------------------------------------------------------------
|
| 4 |
|
|
-- Copyright (C)2011 Mathias Hörtnagl <mathias.hoertnagl@gmail.comt> --
|
| 5 |
|
|
-- --
|
| 6 |
|
|
-- This program is free software: you can redistribute it and/or modify --
|
| 7 |
|
|
-- it under the terms of the GNU General Public License as published by --
|
| 8 |
|
|
-- the Free Software Foundation, either version 3 of the License, or --
|
| 9 |
|
|
-- (at your option) any later version. --
|
| 10 |
|
|
-- --
|
| 11 |
|
|
-- This program is distributed in the hope that it will be useful, --
|
| 12 |
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of --
|
| 13 |
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the --
|
| 14 |
|
|
-- GNU General Public License for more details. --
|
| 15 |
|
|
-- --
|
| 16 |
|
|
-- You should have received a copy of the GNU General Public License --
|
| 17 |
|
|
-- along with this program. If not, see <http://www.gnu.org/licenses/>. --
|
| 18 |
|
|
--------------------------------------------------------------------------------
|
| 19 |
|
|
library ieee;
|
| 20 |
|
|
use ieee.std_logic_1164.all;
|
| 21 |
|
|
use ieee.numeric_std.all;
|
| 22 |
|
|
|
| 23 |
|
|
library work;
|
| 24 |
|
|
use work.mips1.all;
|
| 25 |
|
|
use work.tcpu.all;
|
| 26 |
|
|
|
| 27 |
|
|
package fcpu is
|
| 28 |
|
|
|
| 29 |
|
|
-----------------------------------------------------------------------------
|
| 30 |
|
|
-- ALU --
|
| 31 |
|
|
-----------------------------------------------------------------------------
|
| 32 |
|
|
function addsub (l,r : std_logic_vector; op : alu_op_t)
|
| 33 |
|
|
return std_logic_vector;
|
| 34 |
|
|
function fslt (l,r : std_logic_vector) return std_logic_vector;
|
| 35 |
|
|
function fsltu (l,r : std_logic_vector) return std_logic_vector;
|
| 36 |
|
|
function fsll (l,s : std_logic_vector) return std_logic_vector;
|
| 37 |
|
|
function fsrl (l,s : std_logic_vector) return std_logic_vector;
|
| 38 |
|
|
function fsra (l,s : std_logic_vector) return std_logic_vector;
|
| 39 |
|
|
|
| 40 |
|
|
-----------------------------------------------------------------------------
|
| 41 |
|
|
-- Extend --
|
| 42 |
|
|
-----------------------------------------------------------------------------
|
| 43 |
|
|
function zext (a : std_logic_vector; l : integer) return std_logic_vector;
|
| 44 |
|
|
function sext (a : std_logic_vector; l : integer) return std_logic_vector;
|
| 45 |
|
|
|
| 46 |
|
|
-----------------------------------------------------------------------------
|
| 47 |
|
|
-- Decode --
|
| 48 |
|
|
-----------------------------------------------------------------------------
|
| 49 |
|
|
function link (v_i : de_t) return de_t;
|
| 50 |
|
|
function load (v_i : de_t) return de_t;
|
| 51 |
|
|
function store (v_i : de_t) return de_t;
|
| 52 |
|
|
function simm (v_i : de_t) return de_t;
|
| 53 |
|
|
function zimm (v_i : de_t) return de_t;
|
| 54 |
|
|
|
| 55 |
|
|
-----------------------------------------------------------------------------
|
| 56 |
|
|
-- Clear Pipeline --
|
| 57 |
|
|
-----------------------------------------------------------------------------
|
| 58 |
|
|
function clear (v_i : fe_t) return fe_t;
|
| 59 |
|
|
function clear (v_i : de_t) return de_t;
|
| 60 |
|
|
function clear (v_i : ex_t) return ex_t;
|
| 61 |
|
|
function clear (v_i : me_t) return me_t;
|
| 62 |
|
|
|
| 63 |
|
|
-----------------------------------------------------------------------------
|
| 64 |
|
|
-- Co-Processor 0 --
|
| 65 |
|
|
-----------------------------------------------------------------------------
|
| 66 |
|
|
function clear (v_i : cp0_t) return cp0_t;
|
| 67 |
|
|
function push_ie(v_i : cp0_t) return cp0_t;
|
| 68 |
|
|
function pop_ie(v_i : cp0_t) return cp0_t;
|
| 69 |
|
|
--function set_sr(v_i : cp0_t; v_j : comb_cp0_t) return cp0_t;
|
| 70 |
|
|
function get_sr(v_i : cp0_t) return std_logic_vector;
|
| 71 |
|
|
end fcpu;
|
| 72 |
|
|
|
| 73 |
|
|
package body fcpu is
|
| 74 |
|
|
|
| 75 |
|
|
-----------------------------------------------------------------------------
|
| 76 |
|
|
-- Adder/Subtractor (signed) --
|
| 77 |
|
|
-----------------------------------------------------------------------------
|
| 78 |
|
|
-- Compound Adder/Subtractor (saves LUTs).
|
| 79 |
|
|
function addsub (l,r : std_logic_vector; op : alu_op_t)
|
| 80 |
|
|
return std_logic_vector is
|
| 81 |
|
|
begin
|
| 82 |
|
|
case op is
|
| 83 |
|
|
when ADD | ADDU => return std_logic_vector(signed(l) + signed(r));
|
| 84 |
|
|
when others => return std_logic_vector(signed(l) - signed(r));
|
| 85 |
|
|
end case;
|
| 86 |
|
|
end addsub;
|
| 87 |
|
|
|
| 88 |
|
|
-----------------------------------------------------------------------------
|
| 89 |
|
|
-- Set Less Than Functions --
|
| 90 |
|
|
-----------------------------------------------------------------------------
|
| 91 |
|
|
function fslt (l,r : std_logic_vector) return std_logic_vector is
|
| 92 |
|
|
variable o : std_logic_vector(l'length-1 downto 0) := (others => '0');
|
| 93 |
|
|
begin
|
| 94 |
|
|
if signed(l) < signed(r) then o(0) := '1'; end if;
|
| 95 |
|
|
return o;
|
| 96 |
|
|
end fslt;
|
| 97 |
|
|
|
| 98 |
|
|
function fsltu (l,r : std_logic_vector) return std_logic_vector is
|
| 99 |
|
|
variable o : std_logic_vector(l'length-1 downto 0) := (others => '0');
|
| 100 |
|
|
begin
|
| 101 |
|
|
if unsigned(l) < unsigned(r) then o(0) := '1'; end if;
|
| 102 |
|
|
return o;
|
| 103 |
|
|
end fsltu;
|
| 104 |
|
|
|
| 105 |
|
|
-----------------------------------------------------------------------------
|
| 106 |
|
|
-- Shift (Left, Right Logic, Right Arithmetic) --
|
| 107 |
|
|
-----------------------------------------------------------------------------
|
| 108 |
|
|
function fsll (l,s : std_logic_vector) return std_logic_vector is
|
| 109 |
|
|
variable sh : natural range 0 to l'length-1;
|
| 110 |
|
|
begin
|
| 111 |
|
|
sh := to_integer(unsigned(s));
|
| 112 |
|
|
return std_logic_vector(shift_left(unsigned(l), sh));
|
| 113 |
|
|
end fsll;
|
| 114 |
|
|
|
| 115 |
|
|
function fsrl (l,s : std_logic_vector) return std_logic_vector is
|
| 116 |
|
|
variable sh : natural range 0 to l'length-1;
|
| 117 |
|
|
begin
|
| 118 |
|
|
sh := to_integer(unsigned(s));
|
| 119 |
|
|
return std_logic_vector(shift_right(unsigned(l), sh));
|
| 120 |
|
|
end fsrl;
|
| 121 |
|
|
|
| 122 |
|
|
function fsra (l,s : std_logic_vector) return std_logic_vector is
|
| 123 |
|
|
variable sh : natural range 0 to l'length-1;
|
| 124 |
|
|
begin
|
| 125 |
|
|
sh := to_integer(unsigned(s));
|
| 126 |
|
|
return std_logic_vector(shift_right(signed(l), sh));
|
| 127 |
|
|
end fsra;
|
| 128 |
|
|
|
| 129 |
|
|
-----------------------------------------------------------------------------
|
| 130 |
|
|
-- Extend --
|
| 131 |
|
|
-----------------------------------------------------------------------------
|
| 132 |
|
|
-- Zero extend vector.
|
| 133 |
|
|
function zext (a : std_logic_vector; l : integer) return std_logic_vector is
|
| 134 |
|
|
begin
|
| 135 |
|
|
return std_logic_vector(resize(unsigned(a), l));
|
| 136 |
|
|
end zext;
|
| 137 |
|
|
|
| 138 |
|
|
-- Sign extend vector.
|
| 139 |
|
|
function sext (a : std_logic_vector; l : integer) return std_logic_vector is
|
| 140 |
|
|
begin
|
| 141 |
|
|
return std_logic_vector(resize(signed(a), l));
|
| 142 |
|
|
end sext;
|
| 143 |
|
|
|
| 144 |
|
|
-----------------------------------------------------------------------------
|
| 145 |
|
|
-- Decode --
|
| 146 |
|
|
-----------------------------------------------------------------------------
|
| 147 |
|
|
-- JAL, JRAL, BGEZAL, BLTZAL operations.
|
| 148 |
|
|
function link (v_i : de_t) return de_t is
|
| 149 |
|
|
variable v : de_t := v_i;
|
| 150 |
|
|
begin
|
| 151 |
|
|
v.ec.alu.op := ADDU;
|
| 152 |
|
|
v.ec.alu.src.a := ADD_4;
|
| 153 |
|
|
v.ec.alu.src.b := PC;
|
| 154 |
|
|
v.wc.we := '1';
|
| 155 |
|
|
return v;
|
| 156 |
|
|
end link;
|
| 157 |
|
|
|
| 158 |
|
|
-- Memory load operation setter.
|
| 159 |
|
|
function load (v_i : de_t) return de_t is
|
| 160 |
|
|
variable v : de_t := v_i;
|
| 161 |
|
|
begin
|
| 162 |
|
|
v.ec.wbr := RT;
|
| 163 |
|
|
v.ec.alu.op := ADDU;
|
| 164 |
|
|
v.ec.alu.src.b := SIGN;
|
| 165 |
|
|
v.mc.src := MEM;
|
| 166 |
|
|
v.wc.we := '1';
|
| 167 |
|
|
return v;
|
| 168 |
|
|
end load;
|
| 169 |
|
|
|
| 170 |
|
|
-- Memory store operation setter.
|
| 171 |
|
|
function store (v_i : de_t) return de_t is
|
| 172 |
|
|
variable v : de_t := v_i;
|
| 173 |
|
|
begin
|
| 174 |
|
|
v.ec.alu.op := ADDU;
|
| 175 |
|
|
v.ec.alu.src.b := SIGN;
|
| 176 |
|
|
v.mc.mem.we := '1';
|
| 177 |
|
|
return v;
|
| 178 |
|
|
end store;
|
| 179 |
|
|
|
| 180 |
|
|
-- Sign immediate operation setter.
|
| 181 |
|
|
function simm (v_i : de_t) return de_t is
|
| 182 |
|
|
variable v : de_t := v_i;
|
| 183 |
|
|
begin
|
| 184 |
|
|
v.ec.wbr := RT;
|
| 185 |
|
|
v.ec.alu.src.b := SIGN;
|
| 186 |
|
|
v.wc.we := '1';
|
| 187 |
|
|
return v;
|
| 188 |
|
|
end simm;
|
| 189 |
|
|
|
| 190 |
|
|
-- Zero immediate operation setter.
|
| 191 |
|
|
function zimm (v_i : de_t) return de_t is
|
| 192 |
|
|
variable v : de_t := v_i;
|
| 193 |
|
|
begin
|
| 194 |
|
|
v.ec.wbr := RT;
|
| 195 |
|
|
v.ec.alu.src.b := ZERO;
|
| 196 |
|
|
v.wc.we := '1';
|
| 197 |
|
|
return v;
|
| 198 |
|
|
end zimm;
|
| 199 |
|
|
|
| 200 |
|
|
-----------------------------------------------------------------------------
|
| 201 |
|
|
-- Clear Pipeline --
|
| 202 |
|
|
-----------------------------------------------------------------------------
|
| 203 |
|
|
function clear (v_i : fe_t) return fe_t is
|
| 204 |
|
|
variable v : fe_t := v_i;
|
| 205 |
|
|
begin
|
| 206 |
|
|
v.pc := (others => '0');
|
| 207 |
|
|
return v;
|
| 208 |
|
|
end clear;
|
| 209 |
|
|
|
| 210 |
|
|
function clear (v_i : de_t) return de_t is
|
| 211 |
|
|
variable v : de_t := v_i;
|
| 212 |
|
|
begin
|
| 213 |
|
|
v.cc.mtsr := false;
|
| 214 |
|
|
v.cc.rfe := false;
|
| 215 |
|
|
v.ec.jmp.op := NOP;
|
| 216 |
|
|
v.mc.mem.we := '0';
|
| 217 |
|
|
v.mc.mem.byt := NONE;
|
| 218 |
|
|
v.wc.we := '0';
|
| 219 |
|
|
return v;
|
| 220 |
|
|
end clear;
|
| 221 |
|
|
|
| 222 |
|
|
function clear (v_i : ex_t) return ex_t is
|
| 223 |
|
|
variable v : ex_t := v_i;
|
| 224 |
|
|
begin
|
| 225 |
|
|
v.f.jmp := '0';
|
| 226 |
|
|
v.mc.mem.we := '0';
|
| 227 |
|
|
v.mc.mem.byt := NONE;
|
| 228 |
|
|
v.wc.we := '0';
|
| 229 |
|
|
return v;
|
| 230 |
|
|
end clear;
|
| 231 |
|
|
|
| 232 |
|
|
function clear (v_i : me_t) return me_t is
|
| 233 |
|
|
variable v : me_t := v_i;
|
| 234 |
|
|
begin
|
| 235 |
|
|
v.wc.we := '0';
|
| 236 |
|
|
return v;
|
| 237 |
|
|
end clear;
|
| 238 |
|
|
|
| 239 |
|
|
-----------------------------------------------------------------------------
|
| 240 |
|
|
-- Co-Processor 0 --
|
| 241 |
|
|
-----------------------------------------------------------------------------
|
| 242 |
|
|
-- Clear CP0 status register.
|
| 243 |
|
|
function clear (v_i : cp0_t) return cp0_t is
|
| 244 |
|
|
variable v : cp0_t := v_i;
|
| 245 |
|
|
begin
|
| 246 |
|
|
v.sr.im := x"00";
|
| 247 |
|
|
v.sr.iec := '0';
|
| 248 |
|
|
v.sr.iep := '0';
|
| 249 |
|
|
v.sr.ieo := '0';
|
| 250 |
|
|
return v;
|
| 251 |
|
|
end clear;
|
| 252 |
|
|
|
| 253 |
|
|
-- Push interrupt enable stack.
|
| 254 |
|
|
function push_ie(v_i : cp0_t) return cp0_t is
|
| 255 |
|
|
variable v : cp0_t := v_i;
|
| 256 |
|
|
begin
|
| 257 |
|
|
v.sr.ieo := v_i.sr.iep;
|
| 258 |
|
|
v.sr.iep := v_i.sr.iec;
|
| 259 |
|
|
v.sr.iec := '0';
|
| 260 |
|
|
return v;
|
| 261 |
|
|
end push_ie;
|
| 262 |
|
|
|
| 263 |
|
|
-- Pop interrupt enable stack.
|
| 264 |
|
|
function pop_ie(v_i : cp0_t) return cp0_t is
|
| 265 |
|
|
variable v : cp0_t := v_i;
|
| 266 |
|
|
begin
|
| 267 |
|
|
v.sr.iec := v_i.sr.iep;
|
| 268 |
|
|
v.sr.iep := v_i.sr.ieo;
|
| 269 |
|
|
return v;
|
| 270 |
|
|
end pop_ie;
|
| 271 |
|
|
|
| 272 |
|
|
-- Get status register.
|
| 273 |
|
|
function get_sr(v_i : cp0_t) return std_logic_vector is
|
| 274 |
|
|
variable v : std_logic_vector(31 downto 0) := (others => '0');
|
| 275 |
|
|
begin
|
| 276 |
|
|
v(15 downto 8) := v_i.sr.im;
|
| 277 |
|
|
v(0) := v_i.sr.iec;
|
| 278 |
|
|
v(2) := v_i.sr.iep;
|
| 279 |
|
|
v(4) := v_i.sr.ieo;
|
| 280 |
|
|
return v;
|
| 281 |
|
|
end get_sr;
|
| 282 |
|
|
|
| 283 |
|
|
end fcpu;
|