Mcpu
Mcpu
rev 15102001
5
0102/2001 Tim Boescke
10 /2001 slight changes for proper simulation.
[email protected]
10
library ieee;
use ieee. std logic 1164 . all ;
use ieee. std logic unsigned . all ;
15
entity CPU8BIT2 is
port ( data: inout std logic vector (7 downto 0);
adress: out std logic vector (5 downto 0);
oe: out std logic ;
20 we: out std logic ;
rst : in std logic ;
clk: in std logic );
end;
25 architecture CPU ARCH of CPU8BIT2 is
signal akku: std logic vector (8 downto 0); akku(8) is carry !
signal adreg: std logic vector (5 downto 0);
signal pc: std logic vector (5 downto 0);
signal states : std logic vector (2 downto 0);
30 begin
process(clk,rst)
begin
if (rst = 0) then
adreg <= (others => 0); start execution at memory location 0
35 states <= 000;
akku <= (others => 0);
pc <= (others => 0);
elsif rising edge (clk) then
40 PC / Adress path
if (states = 000) then
pc <= adreg + 1;
adreg <= data(5 downto 0);
else
45 adreg <= pc;
end if;
ALU / Data Path
case states is
50 when 010 => akku <= (0 & akku(7 downto 0)) + (0 & data); add
when 011 => akku(7 downto 0) <= akku(7 downto 0) nor data; nor
when 101 => akku(8) <= 0; branch not taken, clear carry
when others => null; instr. fetch, jcc taken (000), sta (001)
end case;
55
State machine
if (states /= 000) then states <= 000; fetch next opcode
elsif (data(7 downto 6) = 11 and akku(8)=1) then states <= 101; branch n. taken
else states <= 0 & not data(7 downto 6); execute instruction
60 end if;
end if;
end process;
output
65 adress <= adreg;
data <= ZZZZZZZZ when states /= 001 else akku(7 downto 0);
oe <= 1 when (clk=1 or states = 001 or rst=0 or states = 101) else 0;
no memory access during reset and
we <= 1 when (clk=1 or states /= 001 or rst=0) else 0;
70 state 101 (branch not taken)
end CPU ARCH;
5
Listing 3: Verilog version of the CPU, unveried.
//
// Minimal 8 Bit CPU
3 //
// 0102/2001 Tim Boescke
// 10 /2001 changed to synch. reset
// 10 /2004 Verilog version, unveried !
//
8 // [email protected]
//
module vCpu3(data,adress,oe,we,rst,clk);
13 inout [7:0] data;
output [5:0] adress;
output oe;
output we;
input rst ;
18 input clk;
reg [8:0] accumulator; // accumulator(8) is carry !
reg [5:0] adreg;
reg [5:0] pc;
23 reg [2:0] states ;
always @(posedge clk)
if (rst) begin
adreg <= 0;
28 states <= 0;
accumulator <= 0;
end
else begin
// PC / Address path
33 if (| states) begin
pc <= adreg + 1;
adreg <= pc;
end
else adreg <= pc;
38
// ALU / Data Path
case(states)
3b010 : accumulator <= {1b0, accumulator[7:0]} + {1b0, data}; // add
3b011 : accumulator[7:0] <= (accumulator[7:0]|data); // nor
43 3b101 : accumulator[8] <= 1b0; // branch not taken, clear carry
endcase // default : instruction fetch, jcc taken
// State machine
if (| states) states <= 0;
48 else begin
if ( &data[7:6] && accumulator[8] ) states <= 3b101;
else states <= {1b0, data[7:6]};
end
end
53 // output
assign adress = adreg;
assign data = states!=3b001 ? accumulator[7:0] : 8bZZZZZZZZ;
assign oe = clk | rst | (states==3b001) ;
assign we = clk | rst | (states!=3b001) ;
58
endmodule
6