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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use work.alu_types.all;
use work.decoder.all;
entity state_machine is
port(
clk : in std_logic;
opcode : in std_logic_vector(13 downto 0);
reset : in std_logic;
op : out alu_op;
we_mem : out std_logic := '0';
re_mem : out std_logic := '0';
we_w : out std_logic := '0';
instr_ret : out std_logic := '0';
bit_select : out std_logic_vector(2 downto 0) := (others => '0');
data : out std_logic_vector(7 downto 0) := (others => '0');
pc : out std_logic_vector(12 downto 0);
addr : out std_logic_vector(6 downto 0) := (others => '0');
use_literal : out std_logic;
we_status : out std_logic := '0';
stack_push : out std_logic := '0';
stack_pop : out std_logic := '0';
stack_din : out std_logic_vector(12 downto 0) := (others => '0');
stack_dout : in std_logic_vector(12 downto 0);
alu_skip : in std_logic;
alu_result : in std_logic_vector(7 downto 0)
);
end entity state_machine;
architecture rtl of state_machine is
type state_t is (IFetch, MRead, Execute, MWrite);
signal pc_internal : std_logic_vector(12 downto 0) := (others => '0');
signal state : state_t := IFetch;
signal pclath : std_logic_vector(4 downto 0) := (others => '0');
begin
pc <= pc_internal;
fsm : process(clk)
variable instr : instruction_t;
-- do we do a phantom nop after this instruction?
variable do_phantom_nop : boolean := false;
variable is_phantom : boolean := false;
-- true when phantom came from DECFSZ/INCFSZ skip (must still increment PC)
-- false when phantom came from GOTO/CALL/RETURN (PC already at target)
variable is_skip_phantom : boolean := false;
begin
if rising_edge(clk) then
re_mem <= '0';
we_mem <= '0';
we_w <= '0';
we_status <= '0';
instr_ret <= '0';
stack_push <= '0';
stack_pop <= '0';
if reset = '1' then
pc_internal <= (others => '0');
pclath <= (others => '0');
state <= IFetch;
do_phantom_nop := false;
else
case state is
when IFetch =>
-- if we had a branch before, this cycle is a NOP.
if do_phantom_nop then
instr := instruction_decode((others => '0'));
do_phantom_nop := false;
is_phantom := true;
-- is_skip_phantom carries over from whoever set do_phantom_nop
else
instr := instruction_decode(opcode);
is_phantom := false;
is_skip_phantom := false;
end if;
-- set use_literal based on decoded instruction, holds through MWrite
if instr.class = LITERAL_OP then
use_literal <= '1';
else
use_literal <= '0';
end if;
-- for literal operations
data <= instr.k(7 downto 0);
-- for memory-read/write operations
addr <= instr.f;
-- for bit-select operations
bit_select <= instr.b;
state <= Mread;
when MRead =>
-- read memory addressed by the opcode
-- MOVWF, CLRF, CLRW, NOP are write-only.
if instr.class = BIT_OP or
(instr.class = BYTE_OP and
instr.op /= MOVWF and instr.op /= CLRF and
instr.op /= CLRW and instr.op /= NOP) then
re_mem <= '1';
end if;
state <= Execute;
when Execute =>
op <= instr.op; -- move the decoded instruction to the ALU
if not is_phantom then
-- increment PC or change address, different for different instructions.
if instr.op = GOTO then
pc_internal <= "00" & instr.k;
do_phantom_nop := true;
is_skip_phantom := false;
elsif instr.op = CALL then
-- push PC+1 to the stack
stack_din <= std_logic_vector(unsigned(pc_internal) + 1);
stack_push <= '1';
-- go to the function we called
pc_internal <= "00" & instr.k;
do_phantom_nop := true;
is_skip_phantom := false;
elsif instr.op = RETUR or instr.op = RETLW then
-- pop the PC from stack.
pc_internal <= stack_dout;
stack_pop <= '1'; -- pop deferred to next cycle.
do_phantom_nop := true;
is_skip_phantom := false;
else
pc_internal <= std_logic_vector(unsigned(pc_internal) + 1);
end if;
elsif is_skip_phantom then
-- skip phantom: PC was at the skipped instruction, step past it
pc_internal <= std_logic_vector(unsigned(pc_internal) + 1);
end if;
-- branch phantom: PC already at target, no change needed
state <= MWrite;
when MWrite =>
-- write to correct memory based on class (skip for phantom NOP)
if not is_phantom then
case instr.class is
when BYTE_OP =>
if instr.op /= NOP then
if instr.d = '1' then
we_mem <= '1';
-- Write to PCLATH (addr 0x0A): latch upper PC bits
if instr.f = "0001010" then
pclath <= alu_result(4 downto 0);
end if;
-- Write to PCL (addr 0x02): redirect PC, like a GOTO
if instr.f = "0000010" then
pc_internal <= pclath & alu_result;
do_phantom_nop := true;
is_skip_phantom := false;
end if;
else
we_w <= '1';
end if;
end if;
when BIT_OP =>
-- NOT for the BTFSS or BTFSC instructions.
if instr.op = BCF or instr.op = BSF then
we_mem <= '1';
end if;
when LITERAL_OP =>
if instr.op /= NOP and instr.op /= RETUR then
we_w <= '1';
end if;
when others => null;
end case;
end if;
-- DECFSZ/INCFSZ/BTFSS/BTFSC: skip next instruction if ALU signals skip
if not is_phantom and
(instr.op = DECFSZ or instr.op = INCFSZ or instr.op = BTFSS or instr.op = BTFSC) and alu_skip = '1' then
do_phantom_nop := true;
is_skip_phantom := true;
end if;
if not is_phantom then
we_status <= '1';
-- branch instructions complete over 2 cycles; pulse instr_ret only
-- on the phantom NOP cycle (below), not here
if instr.op /= GOTO and instr.op /= CALL and
instr.op /= RETUR and instr.op /= RETLW then
instr_ret <= '1';
end if;
elsif not is_skip_phantom then
-- branch phantom NOP: this is the true end of the 2-cycle instruction
instr_ret <= '1';
end if;
state <= IFetch;
end case;
end if; -- reset
end if;
end process fsm;
end architecture rtl;
|