OSDN Git Service

a86133bea158c1a2ead5370a87d74be899b5c48a
[motonesfpga/motonesfpga.git] / simulation / cpu / mos6502.vhd
1 library ieee;
2 use ieee.std_logic_1164.all;
3
4 entity mos6502 is 
5     generic (   dsize : integer := 8;
6                 asize : integer :=16
7             );
8     port (  input_clk   : in std_logic; --phi0 input pin.
9             rdy         : in std_logic;
10             rst_n       : in std_logic;
11             irq_n       : in std_logic;
12             nmi_n       : in std_logic;
13             dbe         : in std_logic;
14             r_nw        : out std_logic;
15             phi1        : out std_logic;
16             phi2        : out std_logic;
17             addr        : out std_logic_vector ( asize - 1 downto 0);
18             d_io        : inout std_logic_vector ( dsize - 1 downto 0)
19     );
20 end mos6502;
21
22 architecture rtl of mos6502 is
23
24     ----------------------------------------------
25     ------------ decoder declaration -------------
26     ----------------------------------------------
27 component decoder
28     generic (dsize : integer := 8);
29     port (  set_clk         : in std_logic;
30             trig_clk        : in std_logic;
31             res_n           : in std_logic;
32             irq_n           : in std_logic;
33             nmi_n           : in std_logic;
34             rdy             : in std_logic;
35             instruction     : in std_logic_vector (dsize - 1 downto 0);
36             exec_cycle      : in std_logic_vector (5 downto 0);
37             next_cycle      : out std_logic_vector (5 downto 0);
38             status_reg      : inout std_logic_vector (dsize - 1 downto 0);
39             inst_we_n       : out std_logic;
40             ad_oe_n         : out std_logic;
41             dbuf_int_oe_n   : out std_logic;
42             dl_al_we_n      : out std_logic;
43             dl_ah_we_n      : out std_logic;
44             dl_al_oe_n      : out std_logic;
45             dl_ah_oe_n      : out std_logic;
46             dl_dh_oe_n      : out std_logic;
47             pcl_inc_n       : out std_logic;
48             pch_inc_n       : out std_logic;
49             pcl_cmd         : out std_logic_vector(3 downto 0);
50             pch_cmd         : out std_logic_vector(3 downto 0);
51             sp_cmd          : out std_logic_vector(3 downto 0);
52             sph_oe_n        : out std_logic;
53             sp_push_n       : out std_logic;
54             sp_pop_n        : out std_logic;
55             acc_cmd         : out std_logic_vector(3 downto 0);
56             x_cmd           : out std_logic_vector(3 downto 0);
57             y_cmd           : out std_logic_vector(3 downto 0);
58             abs_xy_n        : out std_logic;
59             ea_carry        : in  std_logic;
60             abs_pg_next_n   : out std_logic;
61             zp_n            : out std_logic;
62             zp_xy_n         : out std_logic;
63             stat_dec_oe_n   : out std_logic;
64             stat_bus_oe_n   : out std_logic;
65             stat_set_flg_n  : out std_logic;
66             stat_flg        : out std_logic;
67             stat_bus_all_n  : out std_logic;
68             stat_bus_nz_n   : out std_logic;
69             stat_alu_we_n   : out std_logic;
70             r_nw            : out std_logic
71             ;---for parameter check purpose!!!
72             check_bit     : out std_logic_vector(1 to 5)
73         );
74 end component;
75
76 component alu
77     generic (   dsize : integer := 8
78             );
79     port (  clk             : in std_logic;
80             pcl_inc_n       : in std_logic;
81             pch_inc_n       : in std_logic;
82             sph_oe_n        : in std_logic;
83             sp_push_n       : in std_logic;
84             sp_pop_n        : in std_logic;
85             abs_xy_n        : in std_logic;
86             abs_pg_next_n   : in std_logic;
87             zp_n            : in std_logic;
88             zp_xy_n         : in std_logic;
89             arith_en_n      : in std_logic;
90             instruction     : in std_logic_vector (dsize - 1 downto 0);
91             int_d_bus       : inout std_logic_vector (dsize - 1 downto 0);
92             acc_out         : in std_logic_vector (dsize - 1 downto 0);
93             index_bus       : in std_logic_vector (dsize - 1 downto 0);
94             bal             : in std_logic_vector (dsize - 1 downto 0);
95             bah             : in std_logic_vector (dsize - 1 downto 0);
96             alu_res         : out std_logic_vector (dsize - 1 downto 0);
97             abl             : out std_logic_vector (dsize - 1 downto 0);
98             abh             : out std_logic_vector (dsize - 1 downto 0);
99             pcl_inc_carry   : out std_logic;
100             ea_carry        : out std_logic;
101             carry_in        : in std_logic;
102             negative        : out std_logic;
103             zero            : out std_logic;
104             carry_out       : out std_logic;
105             overflow        : out std_logic
106     );
107 end component;
108
109     ----------------------------------------------
110     ------------ register declaration ------------
111     ----------------------------------------------
112 component d_flip_flop
113     generic (
114             dsize : integer := 8
115             );
116     port (  
117             clk     : in std_logic;
118             res_n   : in std_logic;
119             set_n   : in std_logic;
120             we_n    : in std_logic;
121             d       : in std_logic_vector (dsize - 1 downto 0);
122             q       : out std_logic_vector (dsize - 1 downto 0)
123         );
124 end component;
125
126 component dual_dff
127     generic (
128             dsize : integer := 8
129             );
130     port (  
131             clk             : in std_logic;
132             res_n           : in std_logic;
133             set_n           : in std_logic;
134             gate_cmd        : in std_logic_vector (3 downto 0);
135             front_port      : inout std_logic_vector (dsize - 1 downto 0);
136             back_in_port    : in std_logic_vector (dsize - 1 downto 0);
137             back_out_port   : out std_logic_vector (dsize - 1 downto 0)
138         );
139 end component;
140
141 component data_bus_buffer
142     generic (
143             dsize : integer := 8
144             );
145     port (  
146             clk         : in std_logic;
147             r_nw        : in std_logic;
148             int_oe_n    : in std_logic;
149             int_dbus : inout std_logic_vector (dsize - 1 downto 0);
150             ext_dbus : inout std_logic_vector (dsize - 1 downto 0)
151         );
152 end component;
153
154 component input_data_latch
155     generic (
156             dsize : integer := 8
157             );
158     port (  
159             clk         : in std_logic;
160             oe_n        : in std_logic;
161             we_n        : in std_logic;
162             int_dbus    : in std_logic_vector (dsize - 1 downto 0);
163             alu_bus     : out std_logic_vector (dsize - 1 downto 0)
164         );
165 end component;
166
167 component tri_state_buffer
168     generic (
169             dsize : integer := 8
170             );
171     port (  
172             oe_n    : in std_logic;
173             d       : in std_logic_vector (dsize - 1 downto 0);
174             q       : out std_logic_vector (dsize - 1 downto 0)
175         );
176 end component;
177
178 component processor_status 
179     generic (
180             dsize : integer := 8
181             );
182     port (  
183             clk         : in std_logic;
184             res_n       : in std_logic;
185             dec_oe_n    : in std_logic;
186             bus_oe_n    : in std_logic;
187             set_flg_n   : in std_logic;
188             flg_val     : in std_logic;
189             load_bus_all_n      : in std_logic;
190             load_bus_nz_n       : in std_logic;
191             set_from_alu_n      : in std_logic;
192             alu_n       : in std_logic;
193             alu_v       : in std_logic;
194             alu_z       : in std_logic;
195             alu_c       : in std_logic;
196             dec_val     : inout std_logic_vector (dsize - 1 downto 0);
197             int_dbus    : inout std_logic_vector (dsize - 1 downto 0)
198         );
199 end component;
200
201     ----------------------------------------------
202     ------------ signal declareration ------------
203     ----------------------------------------------
204     signal set_clk : std_logic;
205     signal trigger_clk : std_logic;
206
207     signal exec_cycle : std_logic_vector(5 downto 0);
208     signal next_cycle : std_logic_vector(5 downto 0);
209     signal status_reg : std_logic_vector (dsize - 1 downto 0);
210
211     -------------------------------
212     -------- control lines --------
213     -------------------------------
214     signal inst_we_n : std_logic;
215     signal ad_oe_n : std_logic;
216
217     signal dbuf_r_nw : std_logic;
218     signal dbuf_int_oe_n : std_logic;
219
220     signal dl_al_we_n : std_logic;
221     signal dl_ah_we_n : std_logic;
222     signal dl_al_oe_n : std_logic;
223     signal dl_ah_oe_n : std_logic;
224     signal dl_dh_oe_n : std_logic;
225
226     signal pcl_inc_n : std_logic;
227     signal pch_inc_n : std_logic;
228     signal pcl_inc_carry : std_logic_vector(0 downto 0);
229     signal abs_xy_n        : std_logic;
230     signal ea_carry        : std_logic;
231     signal abs_pg_next_n   : std_logic;
232     signal zp_n            : std_logic;
233     signal zp_xy_n         : std_logic;
234     signal arith_en_n : std_logic;
235                     
236     signal alu_n : std_logic;
237     signal alu_z : std_logic;
238     signal alu_c_in : std_logic;
239     signal alu_c : std_logic;
240     signal alu_v : std_logic;
241
242     ----control line for dual port registers.
243     signal pcl_cmd : std_logic_vector(3 downto 0);
244     signal pch_cmd : std_logic_vector(3 downto 0);
245     signal sp_cmd : std_logic_vector(3 downto 0);
246     signal acc_cmd : std_logic_vector(3 downto 0);
247     signal x_cmd : std_logic_vector(3 downto 0);
248     signal y_cmd : std_logic_vector(3 downto 0);
249     signal sph_oe_n : std_logic;
250     signal sp_push_n : std_logic;
251     signal sp_pop_n  : std_logic;
252
253     ---status register
254     signal stat_dec_oe_n : std_logic;
255     signal stat_bus_oe_n : std_logic;
256     signal stat_set_flg_n : std_logic;
257     signal stat_flg : std_logic;
258     signal stat_bus_all_n : std_logic;
259     signal stat_bus_nz_n : std_logic;
260     signal stat_alu_we_n : std_logic;
261
262     -------------------------------
263     ------------ buses ------------
264     -------------------------------
265     signal instruction : std_logic_vector(dsize - 1 downto 0);
266     
267     signal bah : std_logic_vector(dsize - 1 downto 0);
268     signal bal : std_logic_vector(dsize - 1 downto 0);
269     signal index_bus : std_logic_vector(dsize - 1 downto 0);
270
271     signal pcl_back : std_logic_vector(dsize - 1 downto 0);
272     signal pch_back : std_logic_vector(dsize - 1 downto 0);
273
274     signal acc_out : std_logic_vector(dsize - 1 downto 0);
275
276     signal alu_res : std_logic_vector(dsize - 1 downto 0);
277
278     --not used bus.
279     signal null_bus : std_logic_vector(dsize - 1 downto 0);
280
281     --address bus
282     signal abh : std_logic_vector(dsize - 1 downto 0);
283     signal abl : std_logic_vector(dsize - 1 downto 0);
284
285     ---internal data bus
286     signal d_bus : std_logic_vector(dsize - 1 downto 0);
287
288     ---reset vectors---
289     signal reset_l : std_logic_vector(dsize - 1 downto 0);
290     signal reset_h : std_logic_vector(dsize - 1 downto 0);
291     signal nmi_l : std_logic_vector(dsize - 1 downto 0);
292     signal nmi_h : std_logic_vector(dsize - 1 downto 0);
293     signal irq_l : std_logic_vector(dsize - 1 downto 0);
294     signal irq_h : std_logic_vector(dsize - 1 downto 0);
295     signal rst : std_logic;
296
297     signal check_bit     : std_logic_vector(1 to 5);
298
299 begin
300
301
302     -- clock generate.
303     phi1 <= input_clk;
304     phi2 <= not input_clk;
305     set_clk <= input_clk;
306     trigger_clk <= not input_clk;
307
308     r_nw <= dbuf_r_nw;
309     reset_l <= "00000000";
310     reset_h <= "10000000";
311     rst <= not rst_n;
312
313
314     --------------------------------------------------
315     ------------------- instances --------------------
316     --------------------------------------------------
317
318     dec_inst : decoder generic map (dsize) 
319             port map(set_clk, 
320                     trigger_clk, 
321                     rst_n, 
322                     irq_n, 
323                     nmi_n, 
324                     rdy, 
325                     instruction, 
326                     exec_cycle,
327                     next_cycle,
328                     status_reg, 
329                     inst_we_n, 
330                     ad_oe_n, 
331                     dbuf_int_oe_n,
332                     dl_al_we_n,
333                     dl_ah_we_n,
334                     dl_al_oe_n,
335                     dl_ah_oe_n,
336                     dl_dh_oe_n,
337                     pcl_inc_n,
338                     pch_inc_n,
339                     pcl_cmd,
340                     pch_cmd,
341                     sp_cmd,
342                     sph_oe_n,
343                     sp_push_n,
344                     sp_pop_n,
345                     acc_cmd,
346                     x_cmd,
347                     y_cmd,
348                     abs_xy_n,
349                     ea_carry,
350                     abs_pg_next_n,
351                     zp_n,
352                     zp_xy_n,
353                     stat_dec_oe_n, 
354                     stat_bus_oe_n, 
355                     stat_set_flg_n, 
356                     stat_flg, 
357                     stat_bus_all_n, 
358                     stat_bus_nz_n, 
359                     stat_alu_we_n, 
360                     dbuf_r_nw
361                     , check_bit --check bit.
362                     );
363
364     alu_inst : alu generic map (dsize) 
365             port map (trigger_clk, 
366                     pcl_inc_n,
367                     pch_inc_n,
368                     sph_oe_n,
369                     sp_push_n,
370                     sp_pop_n,
371                     abs_xy_n,
372                     abs_pg_next_n,
373                     zp_n,
374                     zp_xy_n,
375                     arith_en_n,
376                     instruction,
377                     d_bus,
378                     acc_out,
379                     index_bus,
380                     bal,
381                     bah,
382                     alu_res,
383                     abl,
384                     abh,
385                     pcl_inc_carry(0),
386                     ea_carry,
387                     alu_c_in,
388                     alu_n,
389                     alu_z,
390                     alu_c,
391                     alu_v 
392                     );
393
394     --cpu execution cycle number
395     exec_cycle_inst : d_flip_flop generic map (5) 
396             port map(trigger_clk, '1', '1', '0', 
397                     next_cycle(4 downto 0), exec_cycle(4 downto 0));
398
399     exec_cycle_carry_inst : d_flip_flop generic map (1) 
400             port map(trigger_clk, '1', '1', '0', 
401                     pcl_inc_carry, exec_cycle(5 downto 5));
402
403     --io data buffer
404     dbus_buf : data_bus_buffer generic map (dsize) 
405             port map(set_clk, dbuf_r_nw, dbuf_int_oe_n, d_bus, d_io);
406
407     --address operand data buffer.
408     idl_l : input_data_latch generic map (dsize) 
409             port map(set_clk, dl_al_oe_n, dl_al_we_n, d_bus, bal);
410     idl_h : input_data_latch generic map (dsize) 
411             port map(set_clk, dl_ah_oe_n, dl_ah_we_n, d_bus, bah);
412     ---only DLH has b-bus side output.
413     idl_h_d_buf : tri_state_buffer generic map (dsize)
414             port map (dl_dh_oe_n, bah, d_bus);
415
416     -------- registers --------
417     ir : d_flip_flop generic map (dsize) 
418             port map(trigger_clk, '1', '1', inst_we_n, d_io, instruction);
419
420     pcl_inst : dual_dff generic map (dsize) 
421             port map(trigger_clk, '1', rst_n, pcl_cmd, d_bus, pcl_back, bal);
422     pch_inst : dual_dff generic map (dsize) 
423             port map(trigger_clk, '1', rst_n, pch_cmd, d_bus, pch_back, bah);
424
425     --status register
426     status_register : processor_status generic map (dsize) 
427             port map (trigger_clk, rst_n, 
428                     stat_dec_oe_n, stat_bus_oe_n, 
429                     stat_set_flg_n, stat_flg, stat_bus_all_n, stat_bus_nz_n, 
430                     stat_alu_we_n, alu_n, alu_v, alu_z, alu_c,
431                     status_reg, d_bus);
432
433
434     sp : dual_dff generic map (dsize) 
435             port map(trigger_clk, rst_n, '1', sp_cmd, d_bus, alu_res, bal);
436
437     x : dual_dff generic map (dsize) 
438             port map(trigger_clk, rst_n, '1', x_cmd, d_bus, null_bus, index_bus);
439     y : dual_dff generic map (dsize) 
440             port map(trigger_clk, rst_n, '1', y_cmd, d_bus, null_bus, index_bus);
441
442     acc : dual_dff generic map (dsize) 
443             port map(trigger_clk, rst_n, '1', acc_cmd, d_bus, alu_res, acc_out);
444
445     --adh output is controlled by decoder.
446     adh_buf : tri_state_buffer generic map (dsize)
447             port map (ad_oe_n, abh, addr(asize - 1 downto dsize));
448     adl_buf : tri_state_buffer generic map (dsize)
449             port map (ad_oe_n, abl, addr(dsize - 1 downto 0));
450
451     null_bus <= (others => 'Z');
452
453     ----gating reset vector.
454     pcl_buf_alu : tri_state_buffer generic map (dsize)
455             port map (rst, alu_res, pcl_back);
456     pcl_buf_res_vec : tri_state_buffer generic map (dsize)
457             port map (rst_n, reset_l, pcl_back);
458
459     pch_buf_alu : tri_state_buffer generic map (dsize)
460             port map (rst, alu_res, pch_back);
461     pch_buf_res_vec : tri_state_buffer generic map (dsize)
462             port map (rst_n, reset_h, pch_back);
463
464     reset_p : process (rst_n)
465     begin
466         if (rst_n = '0') then
467
468         end if;
469     end process;
470
471
472 ------------------------------------------------------------
473 ------------------------ for debug... ----------------------
474 ------------------------------------------------------------
475
476     dbg_p : process (set_clk)
477 use std.textio.all;
478 use ieee.std_logic_textio.all;
479 use ieee.std_logic_unsigned.conv_integer;
480
481 procedure d_print(msg : string) is
482 variable out_l : line;
483 begin
484     write(out_l, msg);
485     writeline(output, out_l);
486 end  procedure;
487
488 function conv_hex8(ival : integer) return string is
489 variable tmp1, tmp2 : integer;
490 variable hex_chr: string (1 to 16) := "0123456789abcdef";
491 begin
492     tmp2 := (ival mod 16 ** 2) / 16 ** 1;
493     tmp1 := ival mod 16 ** 1;
494     return hex_chr(tmp2 + 1) & hex_chr(tmp1 + 1);
495 end;
496     begin
497         if (set_clk = '0' and exec_cycle = "000000") then
498             --show pc on the T0 (fetch) cycle.
499             d_print("pc : " & conv_hex8(conv_integer(abh)) 
500                     & conv_hex8(conv_integer(abl)));
501         end if;
502     end process;
503
504 end rtl;
505