forth-cpu

Форк
0
/
h2.vhd 
374 строки · 15.5 Кб
1
-------------------------------------------------------------------------------
2
--| @file h2.vhd
3
--| @brief The H2 Processor: J1 processor translation and extension.
4
--| Moved bit 12 to bit 4 to allow for more ALU instructions.
5
--|
6
--| @author         Richard James Howe.
7
--| @copyright      Copyright 2017, 2019 Richard James Howe.
8
--| @license        MIT
9
--| @email          howe.r.j.89@gmail.com
10
--|
11
--| NB. It would be nice to be able to specify the CPU word length with a
12
--| generic, so we could instantiate a 32-bit CPU if we wanted to.
13
-------------------------------------------------------------------------------
14

15
library ieee,work,std;
16
use ieee.std_logic_1164.all;
17
use ieee.numeric_std.all;
18

19
package h2_pkg is
20
	subtype word    is std_ulogic_vector(15 downto 0);
21
	subtype address is std_ulogic_vector(12 downto 0);
22

23
	constant hardware_cpu_id: word   := X"0666";
24
	constant simulation_cpu_id: word := X"1984";
25

26
	component h2 is
27
		generic(
28
			asynchronous_reset:       boolean  := true; -- use asynchronous reset if true, synchronous if false
29
			delay:                    time     := 0 ns; -- simulation only, gate delay
30

31
			cpu_id:                   word     := hardware_cpu_id; -- Value for the CPU ID instruction
32
			interrupt_address_length: positive := 3;               -- Log_2 of the number of interrupts
33
			start_address:            natural  := 0;               -- Initial program counter value
34
			stack_size_log2:          positive := 6;               -- Log_2 of the Size of the stack
35
			use_interrupts:           boolean  := true             -- Enable Interrupts in the H2 Core
36
		);          
37
		port(
38
			clk:      in  std_ulogic;
39
			rst:      in  std_ulogic; -- active high reset, configurable async/sync
40
			stop:     in  std_ulogic; -- Assert high to halt the H2 core
41

42
			-- IO interface
43
			io_wr:    out std_ulogic; -- Output Write Enable
44
			io_re:    out std_ulogic; -- Input  Read  Enable
45
			io_din:   in  word;       -- Data Input from register
46
			io_dout:  out word;       -- Data Output to register
47
			io_daddr: out word;       -- Data Address for I/O action
48

49
			irq:      in  std_ulogic; -- Interrupt Request
50
			irq_addr: in  std_ulogic_vector(interrupt_address_length - 1 downto 0); -- Address to jump to on Interrupt Request
51

52
			-- RAM interface, Dual port
53
			pc:       out address;   -- program counter
54
			insn:     in  word;      -- instruction
55

56
			dwe:      out std_ulogic; -- RAM data write enable
57
			dre:      out std_ulogic; -- RAM data read enable
58
			din:      in  word;       -- RAM data input
59
			dout:     out word;       -- RAM data output
60
			daddr:    out address);   -- RAM address
61
	end component;
62
end;
63

64
library ieee,work,std;
65
use ieee.std_logic_1164.all;
66
use ieee.numeric_std.all;
67
use ieee.math_real.all; -- only needed for calculations relating to generics
68
use work.h2_pkg.all;
69

70
entity h2 is
71
	generic(
72
		asynchronous_reset:       boolean  := true; -- use asynchronous reset if true, synchronous if false
73
		delay:                    time     := 0 ns; -- simulation only, gate delay
74

75
		cpu_id:                   word     := hardware_cpu_id; -- Value for the CPU ID instruction
76
		interrupt_address_length: positive := 3;               -- Log_2 of the number of interrupts
77
		start_address:            natural  := 0;               -- Initial program counter value
78
		stack_size_log2:          positive := 6;               -- Log_2 of the Size of the stack
79
		use_interrupts:           boolean  := true);           -- Enable Interrupts in the H2 Core
80
	port(
81
		clk:      in  std_ulogic;
82
		rst:      in  std_ulogic;
83

84
		-- IO interface
85
		stop:     in  std_ulogic; -- Assert high to halt the H2 core
86

87
		io_wr:    out std_ulogic; -- Output Write Enable
88
		io_re:    out std_ulogic; -- Input  Read  Enable
89
		io_din:   in  word;       -- Data  Input from register
90
		io_dout:  out word;       -- Data  Output to register
91
		io_daddr: out word;       -- Data  Address for I/O action
92

93
		irq:      in  std_ulogic; -- Interrupt Request
94
		irq_addr: in  std_ulogic_vector(interrupt_address_length - 1 downto 0); -- Address to jump to on Interrupt Request
95

96
		-- RAM interface, Dual port
97
		pc:       out address;    -- program counter
98
		insn:     in  word;       -- instruction
99

100
		dwe:      out std_ulogic; -- RAM data write enable
101
		dre:      out std_ulogic; -- RAM data read enable
102
		din:      in  word;       -- RAM data input
103
		dout:     out word;       -- RAM data output
104
		daddr:    out address);   -- RAM address
105
end;
106

107
architecture rtl of h2 is
108
	signal pc_c:        address := std_ulogic_vector(to_unsigned(start_address, address'length));
109
	signal pc_n:        address := (others => '0');
110
	signal pc_plus_one: address := (others => '0');
111

112
	constant stack_size: integer := 2 ** stack_size_log2;
113
	type     stack_type is array (stack_size - 1 downto 0) of word;
114
	subtype  depth is unsigned(stack_size_log2 - 1 downto 0);
115

116
	signal vstkp_c, vstkp_n:  depth := (others => '0');             -- variable stack pointer
117
	signal vstk_ram: stack_type     := (others => (others => '0')); -- variable stack
118
	signal dstk_we: std_ulogic      := '0';                         -- variable stack write enable
119
	signal dd: depth                := (others => '0');             -- variable stack delta
120

121
	signal rstkp_c, rstkp_n:  depth := (others => '0');             -- return stack pointer
122
	signal rstk_ram: stack_type     := (others => (others => '0')); -- return stack
123
	signal rstk_we: std_ulogic      := '0';                         -- return stack write enable
124
	signal rd: depth                := (others => '0');             -- return stack delta
125

126
	type instruction_info_type is record
127
		alu:     std_ulogic;
128
		lit:     std_ulogic;
129
		branch:  std_ulogic;
130
		branch0: std_ulogic;
131
		call:    std_ulogic;
132
	end record;
133

134
	signal is_instr: instruction_info_type := ('0', '0', '0', '0', '0');
135
	signal is_interrupt: std_ulogic := '0';
136
	signal is_ram_write: std_ulogic := '0';
137

138
	type compare_type is record
139
		more:  std_ulogic; -- signed greater than; T > N?
140
		equal: std_ulogic; -- equality; N = T?
141
		umore: std_ulogic; -- unsigned greater than; T > N?
142
		zero:  std_ulogic; -- zero test; T = 0?
143
	end record;
144

145
	signal compare: compare_type := ('0', '0', '0', '0');
146

147
	signal stop_c:     std_ulogic := '1'; -- processor wait state register (current)
148
	signal stop_n:     std_ulogic := '0'; -- processor wait state register (next)
149

150
	signal irq_en_c, irq_en_n: std_ulogic := '0'; -- interrupt enable
151
	signal irq_c, irq_n:       std_ulogic := '0'; -- pending interrupt request
152
	signal irq_addr_c, irq_addr_n: std_ulogic_vector(irq_addr'range) := (others => '0'); -- address of pending interrupt request vector
153

154
	signal tos_c, tos_n: word := (others => '0'); -- top of stack
155
	signal nos:          word := (others => '0'); -- next on stack
156
	signal rtos_c:       word := (others => '0'); -- top of return stack
157
	signal rstk_data:    word := (others => '0'); -- return stack input
158
	signal aluop:        std_ulogic_vector(4 downto 0) := (others => '0'); -- ALU operation
159

160
	signal instruction:  word := (others => '0'); -- processed 'insn'
161
begin
162
	assert stack_size > 4 report "stack size too small: " & integer'image(stack_size) severity failure;
163
	-- assert dd /= "10" severity warning; -- valid, but odd (now used)
164

165
	is_instr.branch  <= '1' when instruction(15 downto 13) = "000" else '0' after delay;
166
	is_instr.branch0 <= '1' when instruction(15 downto 13) = "001" else '0' after delay;
167
	is_instr.call    <= '1' when instruction(15 downto 13) = "010" else '0' after delay;
168
	is_instr.alu     <= '1' when instruction(15 downto 13) = "011" else '0' after delay;
169
	is_instr.lit     <= '1' when instruction(15)           = '1'   else '0' after delay;
170
	is_ram_write     <= '1' when is_instr.alu = '1' and instruction(5) = '1' else '0' after delay;
171
	compare.more     <= '1' when   signed(tos_c) >   signed(nos) else '0' after delay;
172
	compare.umore    <= '1' when unsigned(tos_c) > unsigned(nos) else '0' after delay;
173
	compare.equal    <= '1' when tos_c = nos else '0' after delay;
174
	compare.zero     <= '1' when unsigned(tos_c(15 downto 0)) = 0 else '0' after delay;
175
	nos              <= vstk_ram(to_integer(vstkp_c)) after delay;
176
	rtos_c           <= rstk_ram(to_integer(rstkp_c)) after delay;
177
	pc               <= pc_n after delay;
178
	pc_plus_one      <= std_ulogic_vector(unsigned(pc_c) + 1) after delay;
179
	dout             <= nos after delay;
180
	daddr            <= tos_c(13 downto 1) when is_ram_write = '1' else tos_n(13 downto 1) after delay;
181
	dwe              <= '1' when is_ram_write = '1' and tos_c(15 downto 14) = "00" else '0' after delay;
182
	dre              <= '1' when                        tos_n(15 downto 14) = "00" else '0' after delay;
183
	io_dout          <= nos after delay;
184
	io_daddr         <= tos_c after delay;
185
	io_wr            <= '1' when is_ram_write = '1' and tos_c(15 downto 14) /= "00" else '0' after delay;
186
	is_interrupt     <= '1' when irq_c = '1' and irq_en_c = '1' and use_interrupts else '0' after delay;
187
	irq_n            <= irq after delay;
188
	irq_addr_n       <= irq_addr after delay;
189
	stop_n           <= stop after delay;
190
	dd(0)            <= instruction(0) after delay;
191
	rd(0)            <= instruction(2) after delay;
192
	dd(dd'high downto 1) <= (others => '1') when instruction(1) = '1' else (others => '0') after delay; -- sign extend
193
	rd(rd'high downto 1) <= (others => '1') when instruction(3) = '1' else (others => '0') after delay; -- sign extend
194
	dstk_we          <= '1' when (is_instr.lit = '1' or (is_instr.alu = '1' and instruction(7) = '1')) else '0' after delay;
195

196
	next_state: process(clk, rst)
197
		procedure reset is
198
		begin
199
			pc_c       <= std_ulogic_vector(to_unsigned(start_address, pc_c'length)) after delay;
200
			stop_c     <= '1' after delay; -- start in stopped state
201
			vstkp_c    <= (others => '0') after delay;
202
			rstkp_c    <= (others => '0') after delay;
203
			tos_c      <= (others => '0') after delay;
204
			irq_addr_c <= (others => '0') after delay;
205
			irq_en_c   <= '0' after delay;
206
			irq_c      <= '0' after delay;
207
		end reset;
208
	begin
209
		if rst = '1' and asynchronous_reset then
210
			reset;
211
		elsif rising_edge(clk) then
212
			if rst = '1' and not asynchronous_reset then
213
				reset;
214
			else
215
				assert stop_c = '0' or (stop_c = '1' and is_instr.branch = '1') severity failure;
216
				assert (not rstk_we = '1') or (((is_instr.alu = '1' and instruction(6) = '1') or is_instr.call = '1')) severity failure;
217
				assert (not dstk_we = '1') or (((is_instr.alu = '1' and instruction(7) = '1') or is_instr.lit = '1')) severity failure;
218

219
				pc_c       <= pc_n after delay;
220
				stop_c     <= stop_n after delay;
221
				vstkp_c    <= vstkp_n after delay;
222
				rstkp_c    <= rstkp_n after delay;
223
				tos_c      <= tos_n after delay;
224
				irq_addr_c <= irq_addr_n after delay;
225
				irq_en_c   <= irq_en_n after delay;
226
				irq_c      <= irq_n after delay;
227
			end if;
228
		end if;
229
	end process;
230

231
	stack_write: process(clk)
232
	begin
233
		if rising_edge(clk) then
234
			if dstk_we = '1' then
235
				vstk_ram(to_integer(vstkp_n)) <= tos_c after delay;
236
			end if;
237
			if rstk_we = '1' then
238
				rstk_ram(to_integer(rstkp_n)) <= rstk_data after delay;
239
			end if;
240
		end if;
241
	end process;
242

243
	decode: process(insn, irq_addr_c, is_interrupt, stop_c, pc_c)
244
	begin
245
		if stop_c = '1' then -- assert a BRANCH instruction to current location on CPU halt
246
			instruction <= "000" & pc_c after delay;
247
		elsif is_interrupt = '1' then -- assemble a CALL instruction on interrupt
248
			instruction                   <= (others => '0') after delay;
249
			instruction(15 downto 13)     <= "010" after delay;      -- turn into a CALL
250
			instruction(irq_addr_c'range) <= irq_addr_c after delay; -- address to call
251
		else
252
			instruction <= insn after delay;
253
		end if;
254
	end process;
255

256
	alu_select: process(instruction, is_instr)
257
	begin
258
		if is_instr.lit = '1' then
259
			aluop <= "10101" after delay;
260
		elsif is_instr.branch0 = '1' then
261
			aluop <= (0 => '1', others => '0') after delay;
262
		elsif is_instr.alu = '1' then
263
			aluop <= instruction(12 downto 8) after delay;
264
		else
265
			aluop <= (others => '0') after delay;
266
		end if;
267
	end process;
268

269
	alu_unit: process(
270
		tos_c, nos, rtos_c,
271
		din, instruction, aluop,
272
		io_din,
273
		vstkp_c, rstkp_c,
274
		compare,
275
		irq_en_c)
276
	begin
277
		io_re    <= '0'; -- hardware reads can have side effects
278
		tos_n    <= tos_c;
279
		irq_en_n <= irq_en_c;
280
		case aluop is
281
		-- Register Operations
282
		when "00000" => tos_n <= tos_c after delay;
283
		when "00001" => tos_n <= nos after delay;
284
		when "01011" => tos_n <= rtos_c after delay;
285
		when "10100" => tos_n <= cpu_id after delay;
286
		when "10101" => tos_n <= "0" & instruction(14 downto 0) after delay; -- undocumented, may be removed
287
		-- Logical Operations
288
		when "00011" => tos_n <= tos_c and nos after delay;
289
		when "00100" => tos_n <= tos_c or  nos after delay;
290
		when "00101" => tos_n <= tos_c xor nos after delay;
291
		when "00110" => tos_n <= not tos_c after delay;
292
		-- Comparison Operations
293
		when "00111" => tos_n <= (others => compare.equal) after delay;
294
		when "01000" => tos_n <= (others => compare.more) after delay;
295
		when "01111" => tos_n <= (others => compare.umore) after delay;
296
		when "10011" => tos_n <= (others => compare.zero) after delay;
297
		-- Arithmetic Operations
298
		when "01001" => tos_n <= word(unsigned(nos) srl to_integer(unsigned(tos_c(3 downto 0)))) after delay;
299
		when "01101" => tos_n <= word(unsigned(nos) sll to_integer(unsigned(tos_c(3 downto 0)))) after delay;
300
		when "00010" => tos_n <= word(unsigned(nos) + unsigned(tos_c)) after delay;
301
		when "01010" => tos_n <= word(unsigned(tos_c) - 1) after delay;
302
		-- Input (output is handled elsewhere)
303
		when "01100" => -- input: 0x4000 - 0x7FFF is external input
304
			if tos_c(15 downto 14) /= "00" then
305
				tos_n <= io_din after delay;
306
				io_re <= '1' after delay;
307
			else
308
				tos_n <= din after delay;
309
			end if;
310
		-- Stack Depth
311
		when "01110" => tos_n <= (others => '0') after delay;
312
				tos_n(vstkp_c'range) <= std_ulogic_vector(vstkp_c) after delay;
313
		when "10010" => tos_n <= (others => '0') after delay;
314
				tos_n(rstkp_c'range) <= std_ulogic_vector(rstkp_c) after delay;
315
		-- CPU Status Set/Get
316
		when "10001" => tos_n    <= (others => '0') after delay;
317
				tos_n(0) <= irq_en_c after delay;
318
		when "10000" => tos_n    <= nos after delay;
319
				irq_en_n <= tos_c(0) after delay;
320
		-- Default/Invalid instructions
321
		when others  => tos_n <= tos_c after delay;
322
				report "Invalid ALU operation: " & integer'image(to_integer(unsigned(aluop))) severity error;
323
		end case;
324
	end process;
325

326
	stack_update: process(
327
		pc_c, instruction, tos_c,
328
		vstkp_c, dd,
329
		rstkp_c, rd,
330
		is_instr, pc_plus_one, is_interrupt)
331
	begin
332
		vstkp_n   <= vstkp_c;
333
		rstkp_n   <= rstkp_c;
334
		rstk_we   <= '0';
335
		rstk_data <= "00" & pc_plus_one & "0";
336

337
		if is_instr.lit = '1' then
338
			assert to_integer(vstkp_c) + 1 < stack_size;
339
			vstkp_n   <= vstkp_c + 1 after delay;
340
		end if;
341
		if is_instr.alu = '1' then
342
			assert (not instruction(6) = '1') or ((to_integer(rstkp_c) + to_integer(signed(rd))) < stack_size);
343
			assert ((to_integer(vstkp_c) + to_integer(signed(dd))) < stack_size);
344
			rstk_we   <= instruction(6) after delay;
345
			rstk_data <= tos_c after delay;
346
			vstkp_n   <= vstkp_c + unsigned(dd) after delay;
347
			rstkp_n   <= rstkp_c + unsigned(rd) after delay;
348
		end if;
349
		if is_instr.branch0 = '1' then
350
			vstkp_n   <= (vstkp_c - 1) after delay;
351
		end if;
352
		if is_instr.call = '1' then
353
			if is_interrupt = '1' then
354
				rstk_data <= "00" & pc_c & "0" after delay;
355
			end if;
356
			rstkp_n   <= rstkp_c + 1 after delay;
357
			rstk_we   <= '1' after delay;
358
		end if;
359
	end process;
360

361
	pc_update: process(
362
		instruction, rtos_c, pc_plus_one,
363
		is_instr,
364
		compare.zero)
365
	begin
366
		if is_instr.branch = '1' or (is_instr.branch0 = '1' and compare.zero = '1') or is_instr.call = '1' then
367
			pc_n <=  instruction(12 downto 0) after delay;
368
		elsif is_instr.alu = '1' and instruction(4) = '1' then
369
			pc_n <=  rtos_c(13 downto 1) after delay;
370
		else
371
			pc_n <= pc_plus_one after delay;
372
		end if;
373
	end process;
374
end architecture;
375

376

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.