📄 sparc_disas.vhd
字号:
-------------------------------------------------------------------------------- This file is a part of the GRLIB VHDL IP LIBRARY-- Copyright (C) 2003, Gaisler Research---- This program is free software; you can redistribute it and/or modify-- it under the terms of the GNU General Public License as published by-- the Free Software Foundation; either version 2 of the License, or-- (at your option) any later version.---- This program is distributed in the hope that it will be useful,-- but WITHOUT ANY WARRANTY; without even the implied warranty of-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the-- GNU General Public License for more details.---- You should have received a copy of the GNU General Public License-- along with this program; if not, write to the Free Software-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ------------------------------------------------------------------------------- Package: sparc_disas-- File: sparc_disas.vhd-- Author: Jiri Gaisler, Gaisler Research-- Description: SPARC disassembler according to SPARC V8 manual -------------------------------------------------------------------------------- pragma translate_offlibrary ieee;use ieee.std_logic_1164.all;use ieee.numeric_std.all;library grlib;use grlib.stdlib.all;use grlib.sparc.all;use std.textio.all;package sparc_disas is function tostf(v:std_logic_vector) return string; procedure print_insn(ndx: integer; pc, op, res : std_logic_vector(31 downto 0); valid, trap, wr, rest : boolean); procedure print_fpinsn(ndx: integer; pc, op : std_logic_vector(31 downto 0); res : std_logic_vector(63 downto 0); dpres, valid, trap, wr : boolean); function ins2st(pc, op : std_logic_vector(31 downto 0)) return string;end;package body sparc_disas istype base_type is (hex, dec);subtype nibble is std_logic_vector(3 downto 0);type pc_op_type is record pc, op : std_logic_vector(31 downto 0);end record;function tostd(v:std_logic_vector) return string;function tosth(v:std_logic_vector) return string;function tostrd(n:integer) return string;function tohex(n:nibble) return character isbegin case n is when "0000" => return('0'); when "0001" => return('1'); when "0010" => return('2'); when "0011" => return('3'); when "0100" => return('4'); when "0101" => return('5'); when "0110" => return('6'); when "0111" => return('7'); when "1000" => return('8'); when "1001" => return('9'); when "1010" => return('a'); when "1011" => return('b'); when "1100" => return('c'); when "1101" => return('d'); when "1110" => return('e'); when "1111" => return('f'); when others => return('X'); end case;end;type carr is array (0 to 9) of character;constant darr : carr := ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');function tostd(v:std_logic_vector) return string isvariable s : string(1 to 2);variable val : integer;begin val := conv_integer(v); s(1) := darr(val / 10); s(2) := darr(val mod 10); return(s);end;function tosth(v:std_logic_vector) return string isconstant vlen : natural := v'length; --'constant slen : natural := (vlen+3)/4;variable vv : std_logic_vector(vlen-1 downto 0);variable s : string(1 to slen);begin vv := v; for i in slen downto 1 loop s(i) := tohex(vv(3 downto 0)); vv(vlen-5 downto 0) := vv(vlen-1 downto 4); end loop; return(s);end;function tostf(v:std_logic_vector) return string isconstant vlen : natural := v'length; --'constant slen : natural := (vlen+3)/4;variable vv : std_logic_vector(vlen-1 downto 0);variable s : string(1 to slen);begin vv := v; for i in slen downto 1 loop s(i) := tohex(vv(3 downto 0)); vv(vlen-5 downto 0) := vv(vlen-1 downto 4); end loop; return("0x" & s);end;function tostrd(n:integer) return string isvariable len : integer := 0;variable tmp : string(10 downto 1);variable v : integer := n;begin for i in 0 to 9 loop tmp(i+1) := darr(v mod 10); if tmp(i+1) /= '0' then len := i; end if; v := v/10; end loop; return(tmp(len+1 downto 1));end;function ireg2st(v : std_logic_vector) return string is variable ctmp : character; variable reg : std_logic_vector(4 downto 0); begin reg := v; case reg(4 downto 3) is when "00" => ctmp := 'g'; when "01" => ctmp := 'o'; when "10" => ctmp := 'l'; when "11" => ctmp := 'i'; when others => ctmp := 'X'; end case; if v(4 downto 0) = "11110" then return("%fp"); elsif v(4 downto 0) = "01110" then return("%sp"); else return('%' & ctmp & tost('0' & reg(2 downto 0))); end if;end;function simm13dec(insn : pc_op_type; base : base_type; merge : boolean) return string is variable simm : std_logic_vector(12 downto 0) := insn.op(12 downto 0); variable rs1 : std_logic_vector(4 downto 0) := insn.op(18 downto 14); variable i : std_ulogic := insn.op(13); variable sig : character; variable fill : std_logic_vector(31 downto 13) := (others => simm(12));begin if i = '0' then return(""); else if (simm(12) = '1') and (base = dec) then sig := '-'; simm := (not simm) + 1; else sig := '+'; end if; if base = dec then if merge then if rs1 = "00000" then return(tost(simm)); else return(sig & tost(simm)); end if; else if rs1 = "00000" then return(tost(simm)); else if sig = '-' then return(", " & sig & tost(simm)); else return(", " & tost(simm)); end if; end if; end if; else if rs1 = "00000" then if simm(12) = '1' then return(tost(fill & simm)); else return(tost(simm)); end if; else if simm(12) = '1' then return(", " & tost(fill & simm)); else return(", " & tost(simm)); end if; end if; end if; end if;end;function freg2(insn : pc_op_type) return string is variable rs1, rs2, rd : std_logic_vector(4 downto 0); variable i : std_ulogic;begin rs2 := insn.op(4 downto 0); rd := insn.op(29 downto 25); return("%f" & tostd(rs2) & ", %f" & tostd(rd));end;function creg3(insn : pc_op_type) return string is variable rs1, rs2, rd : std_logic_vector(4 downto 0); variable i : std_ulogic;begin rs1 := insn.op(18 downto 14); rs2 := insn.op(4 downto 0); rd := insn.op(29 downto 25); return("%c" & tostd(rs1) & ", %c" & tostd(rs2) & ", %c" & tostd(rd));end;function freg3(insn : pc_op_type) return string is variable rs1, rs2, rd : std_logic_vector(4 downto 0); variable i : std_ulogic;begin rs1 := insn.op(18 downto 14); rs2 := insn.op(4 downto 0); rd := insn.op(29 downto 25); return("%f" & tostd(rs1) & ", %f" & tostd(rs2) & ", %f" & tostd(rd));end;function fregc(insn : pc_op_type) return string is variable rs1, rs2 : std_logic_vector(4 downto 0); variable i : std_ulogic;begin rs1 := insn.op(18 downto 14); rs2 := insn.op(4 downto 0); return("%f" & tostd(rs1) & ", %f" & tostd(rs2));end;function regimm(insn : pc_op_type; base : base_type; merge : boolean) return string is variable rs1, rs2 : std_logic_vector(4 downto 0); variable i : std_ulogic;begin rs1 := insn.op(18 downto 14); rs2 := insn.op(4 downto 0); i := insn.op(13); if i = '0' then if (rs1 = "00000") then if (rs2 = "00000") then return("0"); else return(ireg2st(rs2)); end if; else if (rs2 = "00000") then return(ireg2st(rs1)); elsif merge then return(ireg2st(rs1) & " + " & ireg2st(rs2)); else return(ireg2st(rs1) & ", " & ireg2st(rs2)); end if; end if; else if (rs1 = "00000") then return(simm13dec(insn, base, merge)); elsif insn.op(12 downto 0) = "0000000000000" then return(ireg2st(rs1)); else return(ireg2st(rs1) & simm13dec(insn, base, merge)); end if; end if;end;function regres(insn : pc_op_type; base : base_type) return string is variable rs1, rs2, rd : std_logic_vector(4 downto 0); variable i : std_ulogic;begin rd := insn.op(29 downto 25); return(regimm(insn, base,false) & ", " & ireg2st(rd ));end;function branchop(insn : pc_op_type) return string is variable simm : std_logic_vector(31 downto 0);begin case insn.op(28 downto 25) is when "0000" => return("n"); when "0001" => return("e"); when "0010" => return("le"); when "0011" => return("l"); when "0100" => return("leu"); when "0101" => return("cs"); when "0110" => return("neg"); when "0111" => return("vs"); when "1000" => return("a"); when "1001" => return("ne"); when "1010" => return("g"); when "1011" => return("ge"); when "1100" => return("gu"); when "1101" => return("cc"); when "1110" => return("pos"); when "1111" => return("vc"); when others => return("XXX"); end case;end;function fbranchop(insn : pc_op_type) return string is variable simm : std_logic_vector(31 downto 0);begin case insn.op(28 downto 25) is when "0000" => return("n"); when "0001" => return("ne"); when "0010" => return("lg"); when "0011" => return("ul"); when "0100" => return("l"); when "0101" => return("ug"); when "0110" => return("g"); when "0111" => return("u"); when "1000" => return("a"); when "1001" => return("e"); when "1010" => return("ue"); when "1011" => return("ge"); when "1100" => return("uge"); when "1101" => return("le"); when "1110" => return("ule"); when "1111" => return("o"); when others => return("XXX"); end case;end;function ldparcp(insn : pc_op_type; rd : std_logic_vector; base : base_type) return string isbegin return("[" & regimm(insn,dec,true) & "]" & ", " & "%c" & tost(rd));end;function ldparf(insn : pc_op_type; rd : std_logic_vector; base : base_type) return string isbegin return("[" & regimm(insn,dec,true) & "]" & ", " & "%f" & tostd(rd));end;function ldpar(insn : pc_op_type; rd : std_logic_vector; base : base_type) return string isbegin return("[" & regimm(insn,dec,true) & "]" & ", " & ireg2st(rd));end;function ldpara(insn : pc_op_type; rd : std_logic_vector; base : base_type) return string isbegin return("[" & regimm(insn,dec,true) & "]" & " " & tost(insn.op(12 downto 5)) & ", " & ireg2st(rd));end;function stparc(insn : pc_op_type; rd : std_logic_vector; base : base_type) return string isbegin if rd = "00000" then return("[" & regimm(insn,dec,true) & "]"); else return(ireg2st(rd) & ", [" & regimm(insn,dec,true) & "]"); end if;end;function stparcp(insn : pc_op_type; rd : std_logic_vector; base : base_type) return string isbegin return("%c" & tost(rd) & ", [" & regimm(insn,dec,true) & "]");end;function stparf(insn : pc_op_type; rd : std_logic_vector; base : base_type) return string isbegin return("%f" & tostd(rd) & ", [" & regimm(insn,dec,true) & "]");end;function stpar(insn : pc_op_type; rd : std_logic_vector; base : base_type) return string isbegin return(ireg2st(rd) & ", [" & regimm(insn,dec,true) & "]");end;function stpara(insn : pc_op_type; rd : std_logic_vector; base : base_type) return string isbegin return(ireg2st(rd) & ", [" & regimm(insn,dec,true) & "]" & " " & tost(insn.op(12 downto 5)));end;function ins2st(pc, op : std_logic_vector(31 downto 0)) return string is constant STMAX : natural := 9; constant bl2 : string(1 to 2) := (others => ' '); constant bb : string(1 to 4) := (others => ' ');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -