OpenCores
URL https://opencores.org/ocsvn/t48/t48/trunk

Subversion Repositories t48

[/] [t48/] [tags/] [rel_0_2_beta/] [rtl/] [vhdl/] [decoder.vhd] - Blame information for rev 38

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 arniml
-------------------------------------------------------------------------------
2
--
3
-- The Decoder unit.
4
-- It decodes the instruction opcodes and executes them.
5
--
6 38 arniml
-- $Id: decoder.vhd,v 1.4 2004-04-04 14:18:53 arniml Exp $
7 4 arniml
--
8
-- Copyright (c) 2004, Arnim Laeuger (arniml@opencores.org)
9
--
10
-- All rights reserved
11
--
12
-- Redistribution and use in source and synthezised forms, with or without
13
-- modification, are permitted provided that the following conditions are met:
14
--
15
-- Redistributions of source code must retain the above copyright notice,
16
-- this list of conditions and the following disclaimer.
17
--
18
-- Redistributions in synthesized form must reproduce the above copyright
19
-- notice, this list of conditions and the following disclaimer in the
20
-- documentation and/or other materials provided with the distribution.
21
--
22
-- Neither the name of the author nor the names of other contributors may
23
-- be used to endorse or promote products derived from this software without
24
-- specific prior written permission.
25
--
26
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27
-- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
28
-- THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29
-- PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
30
-- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31
-- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32
-- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33
-- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34
-- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35
-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36
-- POSSIBILITY OF SUCH DAMAGE.
37
--
38
-- Please report bugs to the author, but before you do so, please
39
-- make sure that this is not a derivative work and that
40
-- you have the latest version of this file.
41
--
42
-- The latest version of this file can be found at:
43
--      http://www.opencores.org/cvsweb.shtml/t48/
44
--
45
-------------------------------------------------------------------------------
46
 
47
library ieee;
48
use ieee.std_logic_1164.all;
49
 
50
use work.t48_pack.word_t;
51
use work.t48_pack.mstate_t;
52
use work.alu_pack.alu_op_t;
53
use work.cond_branch_pack.all;
54
use work.dmem_ctrl_pack.all;
55
use work.pmem_ctrl_pack.all;
56
 
57
entity decoder is
58
 
59
  generic (
60
    -- store mnemonic in flip-flops (registered-out)
61
    register_mnemonic_g   : integer := 1
62
  );
63
 
64
  port (
65
    -- Global Interface -------------------------------------------------------
66
    clk_i                  : in  std_logic;
67
    res_i                  : in  std_logic;
68
    en_clk_i               : in  boolean;
69
    ea_i                   : in  std_logic;
70
    ale_i                  : in  boolean;
71
    int_n_i                : in  std_logic;
72
    t0_dir_o               : out std_logic;
73
    -- T48 Bus Interface ------------------------------------------------------
74
    data_i                 : in  word_t;
75
    data_o                 : out word_t;
76
    stack_high_o           : out boolean;
77
    alu_write_accu_o       : out boolean;
78
    alu_write_shadow_o     : out boolean;
79
    alu_write_temp_reg_o   : out boolean;
80
    alu_read_alu_o         : out boolean;
81
    bus_write_bus_o        : out boolean;
82
    bus_read_bus_o         : out boolean;
83
    dm_write_dmem_addr_o   : out boolean;
84
    dm_write_dmem_o        : out boolean;
85
    dm_read_dmem_o         : out boolean;
86
    p1_write_p1_o          : out boolean;
87
    p1_read_p1_o           : out boolean;
88
    p2_write_p2_o          : out boolean;
89
    p2_write_exp_o         : out boolean;
90
    p2_read_p2_o           : out boolean;
91 21 arniml
    p2_read_exp_o          : out boolean;
92 4 arniml
    pm_write_pcl_o         : out boolean;
93
    pm_read_pcl_o          : out boolean;
94
    pm_write_pch_o         : out boolean;
95
    pm_read_pch_o          : out boolean;
96
    pm_read_pmem_o         : out boolean;
97
    psw_read_psw_o         : out boolean;
98
    psw_read_sp_o          : out boolean;
99
    psw_write_psw_o        : out boolean;
100
    psw_write_sp_o         : out boolean;
101
    -- ALU Interface ----------------------------------------------------------
102
    alu_carry_i            : in  std_logic;
103
    alu_op_o               : out alu_op_t;
104
    alu_use_carry_o        : out boolean;
105 27 arniml
    alu_da_low_o           : out boolean;
106
    alu_da_high_o          : out boolean;
107 38 arniml
    alu_accu_low_o         : out boolean;
108 27 arniml
    alu_p06_temp_reg_o     : out boolean;
109
    alu_p60_temp_reg_o     : out boolean;
110
    alu_da_overflow_i      : in  boolean;
111 4 arniml
    -- BUS Interface ----------------------------------------------------------
112
    bus_output_pcl_o       : out boolean;
113
    bus_bidir_bus_o        : out boolean;
114
    -- Clock Controller Interface ---------------------------------------------
115
    clk_multi_cycle_o      : out boolean;
116
    clk_assert_psen_o      : out boolean;
117
    clk_assert_prog_o      : out boolean;
118
    clk_assert_rd_o        : out boolean;
119
    clk_assert_wr_o        : out boolean;
120
    clk_mstate_i           : in  mstate_t;
121
    clk_second_cycle_i     : in  boolean;
122
    -- Conditional Branch Logic Interface -------------------------------------
123
    cnd_compute_take_o     : out boolean;
124
    cnd_branch_cond_o      : out branch_conditions_t;
125
    cnd_take_branch_i      : in  boolean;
126
    cnd_comp_value_o       : out comp_value_t;
127
    cnd_f1_o               : out std_logic;
128
    cnd_tf_o               : out std_logic;
129
    -- Data Memory Controller Interface ---------------------------------------
130
    dm_addr_type_o         : out dmem_addr_ident_t;
131
    -- Port 1 Interface -------------------------------------------------------
132
    p1_read_reg_o          : out boolean;
133
    -- Port 2 Interface -------------------------------------------------------
134
    p2_read_reg_o          : out boolean;
135
    p2_output_pch_o        : out boolean;
136
    p2_output_exp_o        : out boolean;
137
    -- Program Memory Controller Interface ------------------------------------
138
    pm_inc_pc_o            : out boolean;
139
    pm_write_pmem_addr_o   : out boolean;
140
    pm_addr_type_o         : out pmem_addr_ident_t;
141
    -- Program Status Word Interface ------------------------------------------
142
    psw_special_data_o     : out std_logic;
143
    psw_carry_i            : in  std_logic;
144 27 arniml
    psw_aux_carry_i        : in  std_logic;
145 4 arniml
    psw_f0_i               : in  std_logic;
146
    psw_inc_stackp_o       : out boolean;
147
    psw_dec_stackp_o       : out boolean;
148
    psw_write_carry_o      : out boolean;
149
    psw_write_aux_carry_o  : out boolean;
150
    psw_write_f0_o         : out boolean;
151
    psw_write_bs_o         : out boolean;
152
    -- Timer Interface --------------------------------------------------------
153
    tim_read_timer_o       : out boolean;
154
    tim_write_timer_o      : out boolean;
155
    tim_start_t_o          : out boolean;
156
    tim_start_cnt_o        : out boolean;
157
    tim_stop_tcnt_o        : out boolean;
158
    tim_overflow_i         : in  boolean
159
  );
160
 
161
end decoder;
162
 
163
 
164
use work.t48_pack.all;
165
use work.alu_pack.all;
166
use work.decoder_pack.all;
167
 
168
use work.t48_comp_pack.opc_decoder;
169
use work.t48_comp_pack.int;
170
 
171
architecture rtl of decoder is
172
 
173
  -- Opcode Decoder
174
  signal opc_multi_cycle_s : boolean;
175
  signal opc_read_bus_s    : boolean;
176
  signal opc_inj_int_s     : boolean;
177
  signal opc_opcode_s      : word_t;
178
  signal opc_mnemonic_s    : mnemonic_t;
179
  signal last_cycle_s      : boolean;
180
 
181
  -- state translators
182
  signal assert_psen_s     : boolean;
183
 
184
  -- branch taken handshake
185
  signal branch_taken_s,
186
         branch_taken_q        : boolean;
187
  signal pm_inc_pc_s           : boolean;
188
  signal pm_write_pmem_addr_s  : boolean;
189
  -- additional signal to increment PC during CALL
190
  signal add_inc_pc_s          : boolean;
191
  -- addtional signal to set PC during RET(R)
192
  signal add_write_pmem_addr_s : boolean;
193
 
194
  -- Flag 1
195
  signal clear_f1_s,
196
         cpl_f1_s          : boolean;
197
  signal f1_q              : std_logic;
198
  -- memory bank select
199
  signal clear_mb_s,
200
         set_mb_s          : boolean;
201
  signal mb_q              : std_logic;
202
 
203
  -- T0 direction selection
204
  signal ent0_clk_s        : boolean;
205
  signal t0_dir_q          : std_logic;
206
 
207
  signal data_s            : word_t;
208
  signal read_dec_s        : boolean;
209
 
210
  signal tf_s              : std_logic;
211
 
212
  signal bus_read_bus_s    : boolean;
213
  signal add_read_bus_s    : boolean;
214
 
215
  signal dm_write_dmem_s   : boolean;
216
 
217
  -- interrupt handling
218
  signal jtf_executed_s    : boolean;
219
  signal en_tcnti_s        : boolean;
220
  signal dis_tcnti_s       : boolean;
221
  signal en_i_s            : boolean;
222
  signal dis_i_s           : boolean;
223
  signal tim_int_s         : boolean;
224
  signal retr_executed_s   : boolean;
225
  signal int_executed_s    : boolean;
226
  signal int_pending_s     : boolean;
227
 
228 38 arniml
  -- pragma translate_off
229
  signal istrobe_s         : std_logic;
230
  -- pragma translate_on
231
 
232 4 arniml
begin
233
 
234
  -----------------------------------------------------------------------------
235
  -- Opcode Decoder
236
  -----------------------------------------------------------------------------
237
  opc_decoder_b : opc_decoder
238
    generic map (
239
      register_mnemonic_g => register_mnemonic_g
240
    )
241
    port map (
242
      clk_i         => clk_i,
243
      res_i         => res_i,
244
      en_clk_i      => en_clk_i,
245
      data_i        => data_i,
246
      read_bus_i    => opc_read_bus_s,
247
      inj_int_i     => opc_inj_int_s,
248
      opcode_o      => opc_opcode_s,
249
      mnemonic_o    => opc_mnemonic_s,
250
      multi_cycle_o => opc_multi_cycle_s
251
    );
252
 
253
 
254
  -----------------------------------------------------------------------------
255
  -- Interrupt Controller.
256
  -----------------------------------------------------------------------------
257
  int_b : int
258
    port map (
259
      clk_i           => clk_i,
260
      res_i           => res_i,
261
      en_clk_i        => en_clk_i,
262
      clk_mstate_i    => clk_mstate_i,
263
      jtf_executed_i  => jtf_executed_s,
264
      tim_overflow_i  => tim_overflow_i,
265
      tf_o            => tf_s,
266
      en_tcnti_i      => en_tcnti_s,
267
      dis_tcnti_i     => dis_tcnti_s,
268
      int_n_i         => int_n_i,
269
      ale_i           => ale_i,
270
      last_cycle_i    => last_cycle_s,
271
      en_i_i          => en_i_s,
272
      dis_i_i         => dis_i_s,
273
      ext_int_o       => open,
274
      tim_int_o       => tim_int_s,
275
      retr_executed_i => retr_executed_s,
276
      int_executed_i  => int_executed_s,
277
      int_pending_o   => int_pending_s
278
    );
279
 
280
  last_cycle_s <= not opc_multi_cycle_s or
281
                  (opc_multi_cycle_s and clk_second_cycle_i);
282
 
283
  -----------------------------------------------------------------------------
284
  -- Process machine_cycle
285
  --
286
  -- Purpose:
287
  --   Generates the control signals that are basically needed for the
288
  --   handling of a machine cycle.
289
  --
290
  machine_cycle: process (clk_mstate_i,
291
                          clk_second_cycle_i,
292
                          ea_i,
293
                          assert_psen_s,
294
                          branch_taken_q,
295
                          int_pending_s)
296
 
297
   variable need_address_v      : boolean;
298
 
299
  begin
300
    -- default assignments
301
    clk_assert_psen_o    <= false;
302
    pm_inc_pc_s          <= false;
303
    pm_write_pmem_addr_s <= false;
304
    pm_read_pmem_o       <= false;
305
    bus_output_pcl_o     <= false;
306
    p2_output_pch_o      <= false;
307
    opc_read_bus_s       <= false;
308
    opc_inj_int_s        <= false;
309
    bus_read_bus_s       <= false;
310
 
311
    need_address_v    := not clk_second_cycle_i or
312
                         (clk_second_cycle_i and assert_psen_s);
313
 
314
    case clk_mstate_i is
315
      when MSTATE1 =>
316
        if need_address_v and not int_pending_s then
317
          if ea_i = '0' then
318
            pm_read_pmem_o  <= true;
319
          else
320
            bus_read_bus_s  <= true;
321
          end if;
322
        end if;
323
 
324
        if not clk_second_cycle_i then
325
          if not int_pending_s then
326
            opc_read_bus_s  <= true;
327
          else
328
            opc_inj_int_s   <= true;    -- inject interrupt call
329
          end if;
330
        end if;
331
 
332
      when MSTATE2 =>
333
        if need_address_v and not branch_taken_q and
334
           not int_pending_s then
335
          pm_inc_pc_s       <= true;
336
        end if;
337
 
338
      when MSTATE3 =>
339
        if need_address_v then
340
          pm_write_pmem_addr_s <= true;
341
 
342
          if ea_i = '1' then
343
            bus_output_pcl_o   <= true;
344
          end if;
345
        end if;
346
 
347
      when MSTATE4 =>
348
        if need_address_v and ea_i = '1' then
349
          clk_assert_psen_o <= true;
350
 
351
          p2_output_pch_o  <= true;
352
        end if;
353
 
354
      when MSTATE5 =>
355
        if need_address_v and ea_i = '1' then
356
          clk_assert_psen_o <= true;
357
        end if;
358
 
359
      when others =>
360
        -- pragma translate_off
361
        assert false
362
          report "Unkown machine state!"
363
          severity error;
364
        -- pragma translate_on
365
 
366
    end case;
367
 
368
  end process machine_cycle;
369
  --
370
  -----------------------------------------------------------------------------
371
 
372
 
373
  -----------------------------------------------------------------------------
374
  -- Process decode
375
  --
376
  -- Purpose:
377
  --   Indentifies each single instruction and steps through the related
378
  --   execution sequence.
379
  --
380
  decode: process (alu_carry_i,
381 27 arniml
                   psw_aux_carry_i,
382
                   alu_da_overflow_i,
383 4 arniml
                   clk_mstate_i,
384
                   clk_second_cycle_i,
385
                   cnd_take_branch_i,
386
                   opc_opcode_s,
387
                   opc_mnemonic_s,
388
                   data_i,
389
                   psw_carry_i,
390
                   psw_f0_i,
391
                   f1_q,
392
                   mb_q,
393
                   tim_int_s,
394
                   int_pending_s)
395
 
396
    procedure address_indirect_3_f is
397
    begin
398
      -- apply dmem address from selected register for indirect mode
399
      if opc_opcode_s(3) = '0' then
400
        dm_read_dmem_o       <= true;
401
        dm_write_dmem_addr_o <= true;
402
        dm_addr_type_o       <= DM_PLAIN;
403
      end if;
404
    end;
405
 
406
    procedure and_or_xor_add_4_f is
407
    begin
408
      -- write dmem contents to Temp Reg
409
      dm_read_dmem_o         <= true;
410
      alu_write_temp_reg_o   <= true;
411
    end;
412
 
413
    procedure and_or_xor_add_5_f (alu_op : alu_op_t) is
414
    begin
415
      -- perform ALU operation and store in Accumulator
416
      alu_op_o               <= alu_op;
417
      alu_read_alu_o         <= true;
418
      alu_write_accu_o       <= true;
419
    end;
420
 
421
    procedure cond_jump_c2_m1_f is
422
    begin
423
      -- store address in Program Counter low byte if branch has to
424
      -- be taken
425
      if clk_mstate_i = MSTATE1 and cnd_take_branch_i then
426
        pm_write_pcl_o       <= true;
427
        branch_taken_s       <= true;
428
      end if;
429
    end;
430
 
431
  begin
432
    -- default assignments
433
    data_s                 <= (others => '-');
434
    read_dec_s             <= false;
435
    branch_taken_s         <= false;
436
    clear_f1_s             <= false;
437
    cpl_f1_s               <= false;
438
    clear_mb_s             <= false;
439
    set_mb_s               <= false;
440
    add_inc_pc_s           <= false;
441
    assert_psen_s          <= false;
442
    stack_high_o           <= false;
443
    alu_write_accu_o       <= false;
444
    alu_write_shadow_o     <= false;
445
    alu_write_temp_reg_o   <= false;
446 27 arniml
    alu_p06_temp_reg_o     <= false;
447
    alu_p60_temp_reg_o     <= false;
448 4 arniml
    alu_read_alu_o         <= false;
449
    bus_write_bus_o        <= false;
450
    bus_bidir_bus_o        <= false;
451
    dm_write_dmem_addr_o   <= false;
452
    dm_write_dmem_s        <= false;
453
    dm_read_dmem_o         <= false;
454
    pm_write_pcl_o         <= false;
455
    pm_read_pcl_o          <= false;
456
    pm_write_pch_o         <= false;
457
    pm_read_pch_o          <= false;
458
    pm_addr_type_o         <= PM_PC;
459
    psw_read_psw_o         <= false;
460
    psw_read_sp_o          <= false;
461
    psw_write_psw_o        <= false;
462
    psw_write_sp_o         <= false;
463
    alu_op_o               <= ALU_NOP;
464
    alu_use_carry_o        <= false;
465 27 arniml
    alu_da_low_o           <= false;
466
    alu_da_high_o          <= false;
467 38 arniml
    alu_accu_low_o         <= false;
468 4 arniml
    clk_assert_prog_o      <= false;
469
    clk_assert_rd_o        <= false;
470
    clk_assert_wr_o        <= false;
471
    cnd_branch_cond_o      <= COND_ON_BIT;
472
    cnd_compute_take_o     <= false;
473
    cnd_comp_value_o       <= opc_opcode_s(7 downto 5);
474
    dm_addr_type_o         <= DM_REG;
475
    tim_read_timer_o       <= false;
476
    tim_write_timer_o      <= false;
477
    tim_start_t_o          <= false;
478
    tim_start_cnt_o        <= false;
479
    tim_stop_tcnt_o        <= false;
480
    p1_write_p1_o          <= false;
481
    p1_read_p1_o           <= false;
482
    p1_read_reg_o          <= false;
483
    p2_write_p2_o          <= false;
484
    p2_write_exp_o         <= false;
485
    p2_read_p2_o           <= false;
486
    p2_read_reg_o          <= false;
487 21 arniml
    p2_read_exp_o          <= false;
488
    p2_output_exp_o        <= false;
489 4 arniml
    psw_special_data_o     <= '0';
490
    psw_inc_stackp_o       <= false;
491
    psw_dec_stackp_o       <= false;
492
    psw_write_carry_o      <= false;
493
    psw_write_aux_carry_o  <= false;
494
    psw_write_f0_o         <= false;
495
    psw_write_bs_o         <= false;
496
    jtf_executed_s         <= false;
497
    en_tcnti_s             <= false;
498
    dis_tcnti_s            <= false;
499
    en_i_s                 <= false;
500
    dis_i_s                <= false;
501
    retr_executed_s        <= false;
502
    int_executed_s         <= false;
503
    add_write_pmem_addr_s  <= false;
504
    ent0_clk_s             <= false;
505
    add_read_bus_s         <= false;
506
 
507
    -- prepare potential register indirect address mode
508
    if not clk_second_cycle_i and clk_mstate_i = MSTATE2 then
509
      data_s               <= (others => '0');
510
      if opc_opcode_s(3) = '1' then
511
        data_s(2 downto 0) <= opc_opcode_s(2 downto 0);
512
      else
513
        data_s(2 downto 0) <= "00" & opc_opcode_s(0);
514
      end if;
515
 
516
      read_dec_s           <= true;
517
      dm_write_dmem_addr_o <= true;
518
      dm_addr_type_o       <= DM_REG;
519
    end if;
520
 
521
    case opc_mnemonic_s is
522
 
523
      -- Mnemonic ADD ---------------------------------------------------------
524
      when MN_ADD =>
525
        case clk_mstate_i is
526
          -- read RAM once for indirect address mode
527
          when MSTATE3 =>
528
            address_indirect_3_f;
529
 
530
          -- store data from RAM to Temp Reg
531
          when MSTATE4 =>
532
            and_or_xor_add_4_f;
533
 
534
          -- perform ADD and store in Accumulator
535
          when MSTATE5 =>
536
            and_or_xor_add_5_f(alu_op => ALU_ADD);
537
 
538
            if opc_opcode_s(4) = '1' then
539 27 arniml
              alu_use_carry_o     <= true;
540 4 arniml
            end if;
541
 
542 27 arniml
            psw_special_data_o    <= alu_carry_i;
543
            psw_write_carry_o     <= true;
544
            psw_write_aux_carry_o <= true;
545 4 arniml
 
546
          when others =>
547
            null;
548
 
549
        end case;
550
 
551
      -- Mnemonic ADD_A_DATA --------------------------------------------------
552
      when MN_ADD_A_DATA =>
553 27 arniml
        assert_psen_s               <= true;
554 4 arniml
 
555
        if clk_second_cycle_i then
556
          case clk_mstate_i is
557
            -- write Temp Reg when contents of Program Memory is on bus
558
            when MSTATE1 =>
559 27 arniml
              alu_write_temp_reg_o  <= true;
560 4 arniml
 
561
            -- perform ADD and store in Accumulator
562
            when MSTATE3 =>
563
              and_or_xor_add_5_f(alu_op => ALU_ADD);
564
 
565
              if opc_opcode_s(4) = '1' then
566 27 arniml
                alu_use_carry_o     <= true;
567 4 arniml
              end if;
568
 
569 27 arniml
              psw_special_data_o    <= alu_carry_i;
570
              psw_write_carry_o     <= true;
571
              psw_write_aux_carry_o <= true;
572 4 arniml
 
573
            when others =>
574
              null;
575
 
576
          end case;
577
 
578
        end if;
579
 
580
      -- Mnemonic ANL ---------------------------------------------------------
581
      when MN_ANL =>
582
        case clk_mstate_i is
583
          -- read RAM once for indirect address mode
584
          when MSTATE3 =>
585
            address_indirect_3_f;
586
 
587
          -- store data from RAM to Temp Reg
588
          when MSTATE4 =>
589
            and_or_xor_add_4_f;
590
 
591
          -- perform AND and store in Accumulator
592
          when MSTATE5 =>
593
            and_or_xor_add_5_f(alu_op => ALU_AND);
594
 
595
          when others =>
596
            null;
597
 
598
        end case;
599
 
600
      -- Mnemonic ANL_A_DATA --------------------------------------------------
601
      when MN_ANL_A_DATA =>
602
        assert_psen_s              <= true;
603
 
604
        if clk_second_cycle_i then
605
          case clk_mstate_i is
606
            -- write Temp Reg when contents of Program Memory is on bus
607
            when MSTATE1 =>
608
              alu_write_temp_reg_o <= true;
609
 
610
            -- perform AND and store in Accumulator
611
            when MSTATE3 =>
612
              and_or_xor_add_5_f(alu_op => ALU_AND);
613
 
614
            when others =>
615
              null;
616
 
617
          end case;
618
 
619
        end if;
620
 
621
      -- Mnemonic ANL_EXT -----------------------------------------------------
622
      when MN_ANL_EXT =>
623
        assert_psen_s            <= true;
624
 
625
        if not clk_second_cycle_i then
626
          -- read port to Temp Reg
627
          if clk_mstate_i = MSTATE5 then
628
            if opc_opcode_s(1 downto 0) = "00" then
629
              add_read_bus_s     <= true;
630
            elsif opc_opcode_s(1) = '0' then
631
              p1_read_p1_o       <= true;
632
              p1_read_reg_o      <= true;
633
            else
634
              p2_read_p2_o       <= true;
635
              p2_read_reg_o      <= true;
636
            end if;
637
 
638
            alu_write_temp_reg_o <= true;
639
          end if;
640
 
641
        else
642
          case clk_mstate_i is
643
            -- write shadow Accumulator when contents of Program Memory is
644
            -- on bus
645
            when MSTATE1 =>
646
              alu_write_shadow_o <= true;
647
 
648
            -- loop shadow Accumulator through ALU to prevent update from
649
            -- real Accumulator
650
            when MSTATE2 =>
651
              alu_read_alu_o     <= true;
652
              alu_write_shadow_o <= true;
653
 
654
            -- write result of AND operation back to port
655
            when MSTATE3 =>
656
              alu_op_o           <= ALU_AND;
657
              alu_read_alu_o     <= true;
658
 
659
              if opc_opcode_s(1 downto 0) = "00" then
660
                bus_write_bus_o  <= true;
661
              elsif opc_opcode_s(1) = '0' then
662
                p1_write_p1_o    <= true;
663
              else
664
                p2_write_p2_o    <= true;
665
              end if;
666
 
667
            when others =>
668
              null;
669
 
670
          end case;
671
 
672
        end if;
673
 
674
      -- Mnemonic CALL --------------------------------------------------------
675
      when MN_CALL =>
676
        assert_psen_s              <= true;
677
 
678
        if not clk_second_cycle_i then
679
          case clk_mstate_i is
680
            -- read Stack Pointer and address Data Memory for low byte
681
            -- also increment Program Counter to point to next instruction
682
            when MSTATE3 =>
683
              psw_read_sp_o        <= true;
684
              dm_write_dmem_addr_o <= true;
685
              dm_addr_type_o       <= DM_STACK;
686
 
687
              -- only increment PC if this is not an injected CALL
688
              -- injected CALLS are not located in Program Memory,
689
              -- the PC points already to the instruction to be executed
690
              -- after the interrupt
691
              if not int_pending_s then
692
                add_inc_pc_s       <= true;
693
              end if;
694
 
695
            -- store Program Counter low byte on stack
696
            when MSTATE4 =>
697
              pm_read_pcl_o        <= true;
698
              dm_write_dmem_s      <= true;
699
 
700
            -- store Program Counter high byte and PSW on stack
701
            -- increment Stack pointer
702
            when MSTATE5 =>
703
              psw_read_psw_o       <= true;
704
              pm_read_pch_o        <= true;
705
              dm_write_dmem_addr_o <= true;
706
              dm_addr_type_o       <= DM_STACK_HIGH;
707
              dm_write_dmem_s      <= true;
708
              psw_inc_stackp_o     <= true;
709
 
710
            when others =>
711
              null;
712
 
713
          end case;
714
 
715
        else
716
          case clk_mstate_i is
717
            -- store address in Program Counter low byte
718
            when MSTATE1 =>
719
              pm_write_pcl_o       <= true;
720
              branch_taken_s       <= true;
721
              if int_pending_s then
722
                -- apply low part of vector address manually
723
                data_s             <= (others => '0');
724
                data_s(1 downto 0) <= "11";
725
                if tim_int_s then
726
                  data_s(2)        <= '1';
727
                end if;
728
                read_dec_s         <= true;
729
              end if;
730
 
731
            when MSTATE2 =>
732
              pm_write_pch_o       <= true;
733
              read_dec_s           <= true;
734
              if not int_pending_s then
735
                -- store high part of target address in Program Counter
736
                data_s             <= "0000" & mb_q & opc_opcode_s(7 downto 5);
737
              else
738
                -- apply high part of vector address manually
739
                data_s             <= (others => '0');
740
                int_executed_s     <= true;
741
              end if;
742
 
743
            when others =>
744
              null;
745
 
746
          end case;
747
 
748
        end if;
749
 
750
      -- Mnemonic CLR_A -------------------------------------------------------
751
      when MN_CLR_A =>
752
        -- write CLR output of ALU to Accumulator
753
        if clk_mstate_i = MSTATE3 then
754
          alu_op_o         <= ALU_CLR;
755
          alu_read_alu_o   <= true;
756
          alu_write_accu_o <= true;
757
        end if;
758
 
759
      -- Mnemonic CLR_C -------------------------------------------------------
760
      when MN_CLR_C =>
761
        -- store 0 to Carry
762
        if clk_mstate_i = MSTATE3 then
763
          psw_special_data_o <= '0';
764
          psw_write_carry_o  <= true;
765
        end if;
766
 
767
      -- Mnemonic CLR_F -------------------------------------------------------
768
      when MN_CLR_F =>
769
        -- store 0 to selected flag
770
        if clk_mstate_i = MSTATE3 then
771
          if opc_opcode_s(5) = '0' then
772
            psw_special_data_o <= '0';
773
            psw_write_f0_o     <= true;
774
          else
775
            clear_f1_s         <= true;
776
          end if;
777
 
778
        end if;
779
 
780
      -- Mnemonic CPL_A -------------------------------------------------------
781
      when MN_CPL_A =>
782
        -- write CPL output of ALU to Accumulator
783
        if clk_mstate_i = MSTATE3 then
784
          alu_op_o         <= ALU_CPL;
785
          alu_read_alu_o   <= true;
786
          alu_write_accu_o <= true;
787
        end if;
788
 
789
      -- Mnemnonic CPL_C ------------------------------------------------------
790
      when MN_CPL_C =>
791
        -- write inverse of Carry to PSW
792
        if clk_mstate_i = MSTATE3 then
793
          psw_special_data_o <= not psw_carry_i;
794
          psw_write_carry_o  <= true;
795
        end if;
796
 
797
      -- Mnemonic CPL_F -------------------------------------------------------
798
      when MN_CPL_f =>
799
        -- write inverse of selected flag back to flag
800
        if clk_mstate_i = MSTATE3 then
801
          if opc_opcode_s(5) = '0' then
802
            psw_special_data_o <= not psw_f0_i;
803
            psw_write_f0_o     <= true;
804
          else
805
            cpl_f1_s           <= true;
806
          end if;
807
 
808
        end if;
809
 
810 27 arniml
      -- Mnemonic DA ----------------------------------------------------------
811
      when MN_DA =>
812
        alu_op_o                 <= ALU_ADD;
813
 
814
        case clk_mstate_i is
815
          -- Step 1: Preload Temp Reg with 0x06
816
          when MSTATE3 =>
817
            alu_p06_temp_reg_o   <= true;
818
 
819
          -- Step 2: Check Auxiliary Carry and overflow on low nibble
820
          --         Add 0x06 to shadow Accumulator if one is true
821
          when MSTATE4 =>
822
            alu_da_low_o         <= true;
823
 
824
            if psw_aux_carry_i = '1' or alu_da_overflow_i then
825
              alu_read_alu_o     <= true;
826
              alu_write_shadow_o <= true;
827
            end if;
828
 
829
            -- preload Temp Reg with 0x60
830
            alu_p60_temp_reg_o  <= true;
831
 
832
          -- Step 3: Check overflow on high nibble
833
          --         Add 0x60 to shadow Accumulator if true and store result
834
          --         in Accumulator and PSW (only Carry)
835
          when MSTATE5 =>
836
            alu_da_high_o        <= true;
837
 
838
            if alu_da_overflow_i then
839
              psw_special_data_o <= alu_carry_i;
840
            else
841
              alu_op_o           <= ALU_NOP;
842
              psw_special_data_o <= '0';
843
            end if;
844
            alu_read_alu_o       <= true;
845
            alu_write_accu_o     <= true;
846
            psw_write_carry_o    <= true;
847
 
848
          when others =>
849
            null;
850
 
851
        end case;
852
 
853 4 arniml
      -- Mnemonic DEC ---------------------------------------------------------
854
      when MN_DEC =>
855
        case clk_mstate_i is
856
          when MSTATE4 =>
857
            -- DEC Rr: store data from RAM to shadow Accumulator
858
            if opc_opcode_s(6) = '1' then
859
              dm_read_dmem_o         <= true;
860
              alu_write_shadow_o     <= true;
861
            end if;
862
 
863
          when MSTATE5 =>
864
            alu_op_o                 <= ALU_DEC;
865
            alu_read_alu_o           <= true;
866
 
867
            if opc_opcode_s(6) = '0' then
868
              -- write DEC of Accumulator to Accumulator
869
              alu_write_accu_o       <= true;
870
            else
871
              -- store DEC of shadow Accumulator back to dmem
872
              dm_write_dmem_s        <= true;
873
            end if;
874
 
875
          when others =>
876
            null;
877
 
878
        end case;
879
 
880
      -- Mnemonic DIS_EN_I ----------------------------------------------------
881
      when MN_DIS_EN_I =>
882
        if clk_mstate_i = MSTATE3 then
883
          if opc_opcode_s(4) = '1' then
884
            dis_i_s <= true;
885
          else
886
            en_i_s  <= true;
887
          end if;
888
        end if;
889
 
890
      -- Mnemonic DIS_EN_TCNTI ------------------------------------------------
891
      when MN_DIS_EN_TCNTI =>
892
        if clk_mstate_i = MSTATE3 then
893
          if opc_opcode_s(4) = '1' then
894
            dis_tcnti_s <= true;
895
          else
896
            en_tcnti_s  <= true;
897
          end if;
898
        end if;
899
 
900
      -- Mnemonic DJNZ --------------------------------------------------------
901
      when MN_DJNZ =>
902
        assert_psen_s              <= true;
903
 
904
        if not clk_second_cycle_i then
905
          case clk_mstate_i is
906
            -- store data from RAM to shadow Accumulator
907
            when MSTATE4 =>
908
              dm_read_dmem_o         <= true;
909
              alu_write_shadow_o     <= true;
910
 
911
            -- write DEC result of shadow Accumulator back to dmem and
912
            -- conditional branch logic
913
            when MSTATE5 =>
914
              alu_op_o               <= ALU_DEC;
915
              alu_read_alu_o         <= true;
916
              dm_write_dmem_s        <= true;
917
 
918
              cnd_compute_take_o     <= true;
919
              cnd_branch_cond_o      <= COND_Z;
920
              cnd_comp_value_o(0)    <= '0';
921
 
922
            when others =>
923
              null;
924
 
925
          end case;
926
 
927
        else
928
          -- store address in Program Counter low byte if branch has to
929
          -- be taken
930
          cond_jump_c2_m1_f;
931
 
932
        end if;
933
 
934
      -- Mnemonic ENT0_CLK ----------------------------------------------------
935
      when MN_ENT0_CLK =>
936
        if clk_mstate_i = MSTATE3 then
937
          ent0_clk_s <= true;
938
        end if;
939
 
940
      -- Mnemonic IN ----------------------------------------------------------
941
      when MN_IN =>
942
        -- read Port and store in Accumulator
943
        if clk_second_cycle_i and clk_mstate_i = MSTATE2 then
944
          alu_write_accu_o <= true;
945
 
946
          if opc_opcode_s(1) = '0' then
947
            p1_read_p1_o   <= true;
948
          else
949
            p2_read_p2_o   <= true;
950
          end if;
951
        end if;
952
 
953
      -- Mnemonic INS ---------------------------------------------------------
954
      when MN_INS =>
955
        -- read BUS and store in Accumulator
956
        if clk_second_cycle_i and clk_mstate_i = MSTATE2 then
957
          alu_write_accu_o <= true;
958
 
959
          add_read_bus_s   <= true;
960
        end if;
961
 
962
      -- Mnemonic INC ---------------------------------------------------------
963
      when MN_INC =>
964
        case clk_mstate_i is
965
          -- read RAM once for indirect address mode
966
          when MSTATE3 =>
967
            address_indirect_3_f;
968
 
969
          when MSTATE4 =>
970
            -- INC Rr; INC @ Rr: store data from RAM to shadow Accumulator
971
            if opc_opcode_s(3 downto 2) /= "01" then
972
              dm_read_dmem_o         <= true;
973
              alu_write_shadow_o     <= true;
974
            end if;
975
 
976
          when MSTATE5 =>
977
            alu_op_o                 <= ALU_INC;
978
            alu_read_alu_o           <= true;
979
 
980
            if opc_opcode_s(3 downto 2) = "01" then
981
              -- write INC output of ALU to Accumulator
982
              alu_write_accu_o       <= true;
983
            else
984
              -- store INC of shadow Accumulator back to dmem
985
              dm_write_dmem_s        <= true;
986
            end if;
987
 
988
          when others =>
989
            null;
990
 
991
        end case;
992
 
993
      -- Mnemonic JBB ---------------------------------------------------------
994
      when MN_JBB =>
995
        assert_psen_s          <= true;
996
        cnd_branch_cond_o      <= COND_ON_BIT;
997
 
998
        if not clk_second_cycle_i then
999
          -- read Accumulator and start branch calculation
1000
          if clk_mstate_i = MSTATE3 then
1001
            alu_read_alu_o     <= true;
1002
            cnd_compute_take_o <= true;
1003
            -- cnd_comp_value_o is ok by default assignment
1004
          end if;
1005
 
1006
        else
1007
          -- store address in Program Counter low byte if branch has to
1008
          -- be taken
1009
          cond_jump_c2_m1_f;
1010
 
1011
        end if;
1012
 
1013
      -- Mnemonic JC ----------------------------------------------------------
1014
      when MN_JC =>
1015
        assert_psen_s           <= true;
1016
        cnd_branch_cond_o       <= COND_C;
1017
 
1018
        if not clk_second_cycle_i then
1019
          -- start branch calculation
1020
          if clk_mstate_i = MSTATE3 then
1021
            cnd_compute_take_o  <= true;
1022
            cnd_comp_value_o(0) <= opc_opcode_s(4);
1023
          end if;
1024
 
1025
        else
1026
          -- store address in Program Counter low byte if branch has to
1027
          -- be taken
1028
          cond_jump_c2_m1_f;
1029
 
1030
        end if;
1031
 
1032
      -- Mnemonic JF ----------------------------------------------------------
1033
      when MN_JF =>
1034
        assert_psen_s           <= true;
1035
 
1036
        if not clk_second_cycle_i then
1037
          -- start branch calculation
1038
          if clk_mstate_i = MSTATE3 then
1039
            cnd_compute_take_o  <= true;
1040
            if opc_opcode_s(7) = '1' then
1041
              -- JF0
1042
              cnd_branch_cond_o <= COND_F0;
1043
            else
1044
              -- JF1
1045
              cnd_branch_cond_o <= COND_F1;
1046
            end if;
1047
 
1048
          end if;
1049
 
1050
        else
1051
          -- store address in Program Counter low byte if branch has to
1052
          -- be taken
1053
          cond_jump_c2_m1_f;
1054
 
1055
        end if;
1056
 
1057
 
1058
      -- Mnemonic JMP ---------------------------------------------------------
1059
      when MN_JMP =>
1060
        assert_psen_s        <= true;
1061
 
1062
        if clk_second_cycle_i then
1063
          case clk_mstate_i is
1064
            -- store address in Program Counter low byte
1065
            when MSTATE1 =>
1066
              pm_write_pcl_o <= true;
1067
              branch_taken_s <= true;
1068
 
1069
            -- store high part of target address in Program Counter
1070
            when MSTATE2 =>
1071
              data_s         <= "0000" & mb_q & opc_opcode_s(7 downto 5);
1072
              read_dec_s     <= true;
1073
              pm_write_pch_o <= true;
1074
 
1075
 
1076
            when others =>
1077
              null;
1078
 
1079
          end case;
1080
 
1081
        end if;
1082
 
1083
      -- Mnemonic JMPP --------------------------------------------------------
1084
      when MN_JMPP =>
1085
        assert_psen_s    <= true;
1086
 
1087
        if not clk_second_cycle_i then
1088
          -- write Accumulator to Program Memory address
1089
          -- (skip page offset update from Program Counter)
1090
          if clk_mstate_i = MSTATE3 then
1091
            alu_read_alu_o <= true;
1092
            pm_addr_type_o <= PM_PAGE;
1093
          end if;
1094
 
1095
        else
1096
          if clk_mstate_i = MSTATE1 then
1097
            -- store address in Program Counter low byte
1098
            pm_write_pcl_o <= true;
1099
            branch_taken_s <= true;
1100
          end if;
1101
 
1102
        end if;
1103
 
1104
      -- Mnemonic JNI ---------------------------------------------------------
1105
      when MN_JNI =>
1106
        assert_psen_s          <= true;
1107
        cnd_branch_cond_o      <= COND_INT;
1108
 
1109
        if not clk_second_cycle_i then
1110
          -- start branch calculation
1111
          if clk_mstate_i = MSTATE3 then
1112
            cnd_compute_take_o <= true;
1113
          end if;
1114
 
1115
        else
1116
          -- store address in Program Counter low byte if branch has to
1117
          -- be taken
1118
          cond_jump_c2_m1_f;
1119
 
1120
        end if;
1121
 
1122
      -- Mnemonic JT ----------------------------------------------------------
1123
      when MN_JT =>
1124
        assert_psen_s           <= true;
1125
        if opc_opcode_s(6) = '0' then
1126
          cnd_branch_cond_o     <= COND_T0;
1127
        else
1128
          cnd_branch_cond_o     <= COND_T1;
1129
        end if;
1130
 
1131
        if not clk_second_cycle_i then
1132
          -- start branch calculation
1133
          if clk_mstate_i = MSTATE3 then
1134
            cnd_compute_take_o  <= true;
1135
            cnd_comp_value_o(0) <= opc_opcode_s(4);
1136
          end if;
1137
 
1138
        else
1139
          -- store address in Program Counter low byte if branch has to
1140
          -- be taken
1141
          cond_jump_c2_m1_f;
1142
 
1143
        end if;
1144
 
1145
      -- Mnemonic JTF ---------------------------------------------------------
1146
      when MN_JTF =>
1147
        assert_psen_s          <= true;
1148
        cnd_branch_cond_o      <= COND_TF;
1149
 
1150
        if not clk_second_cycle_i then
1151
          -- start branch calculation
1152
          if clk_mstate_i = MSTATE3 then
1153
            cnd_compute_take_o <= true;
1154
            jtf_executed_s     <= true;
1155
          end if;
1156
 
1157
        else
1158
          -- store address in Program Counter low byte if branch has to
1159
          -- be taken
1160
          cond_jump_c2_m1_f;
1161
 
1162
        end if;
1163
 
1164
      -- Mnemonic JZ ----------------------------------------------------------
1165
      when MN_JZ =>
1166
        assert_psen_s           <= true;
1167
        cnd_branch_cond_o       <= COND_Z;
1168
 
1169
        if not clk_second_cycle_i then
1170
          -- read Accumulator and start branch calculation
1171
          if clk_mstate_i = MSTATE3 then
1172
            alu_read_alu_o      <= true;
1173
            cnd_compute_take_o  <= true;
1174
            cnd_comp_value_o(0) <= opc_opcode_s(6);
1175
          end if;
1176
 
1177
        else
1178
          -- store address in Program Counter low byte if branch has to
1179
          -- be taken
1180
          cond_jump_c2_m1_f;
1181
 
1182
        end if;
1183
 
1184
      -- Mnemonic MOV_A_DATA --------------------------------------------------
1185
      when MN_MOV_A_DATA =>
1186
        assert_psen_s      <= true;
1187
 
1188
        -- Write Accumulator when contents of Program Memory is on bus
1189
        -- during machine state 1 of second cycle.
1190
        if clk_second_cycle_i and clk_mstate_i = MSTATE1 then
1191
          alu_write_accu_o <= true;
1192
        end if;
1193
 
1194
      -- Mnemonic MOV_A_RR ----------------------------------------------------
1195
      when MN_MOV_A_RR =>
1196
        case clk_mstate_i is
1197
          -- read RAM once for indirect address mode
1198
          when MSTATE3 =>
1199
            address_indirect_3_f;
1200
 
1201
          -- read data from RAM and store in Accumulator
1202
          when MSTATE4 =>
1203
            and_or_xor_add_4_f;
1204
            alu_write_accu_o <= true;
1205
 
1206
          when others =>
1207
            null;
1208
 
1209
        end case;
1210
 
1211
      -- Mnemonic MOV_A_PSW ---------------------------------------------------
1212
      when MN_MOV_A_PSW =>
1213
        if clk_mstate_i = MSTATE3 then
1214
          psw_read_psw_o   <= true;
1215
          psw_read_sp_o    <= true;
1216
          alu_write_accu_o <= true;
1217
        end if;
1218
 
1219
      -- Mnemoniv MOV_PSW_A ---------------------------------------------------
1220
      when MN_MOV_PSW_A =>
1221
        if clk_mstate_i = MSTATE3 then
1222
          alu_read_alu_o  <= true;
1223
          psw_write_psw_o <= true;
1224
          psw_write_sp_o  <= true;
1225
        end if;
1226
 
1227
      -- Mnemonic MOV_RR ------------------------------------------------------
1228
      when MN_MOV_RR =>
1229
        case clk_mstate_i is
1230
          -- read RAM once for indirect address mode
1231
          when MSTATE3 =>
1232
            address_indirect_3_f;
1233
 
1234
          -- write Accumulator to dmem
1235
          when MSTATE5 =>
1236
            alu_read_alu_o       <= true;
1237
            dm_write_dmem_s      <= true;
1238
 
1239
          when others =>
1240
            null;
1241
 
1242
        end case;
1243
 
1244
      -- Mnemonic MOV_RR_DATA -------------------------------------------------
1245
      when MN_MOV_RR_DATA =>
1246
        assert_psen_s     <= true;
1247
 
1248
        -- read RAM once for indirect address mode
1249
        if not clk_second_cycle_i and clk_mstate_i = MSTATE3 then
1250
          address_indirect_3_f;
1251
        end if;
1252
 
1253
        -- Write Data Memory when contents of Program Memory is on bus
1254
        -- during machine state 1 of second cycle.
1255
        if clk_second_cycle_i and clk_mstate_i = MSTATE1 then
1256
          dm_write_dmem_s <= true;
1257
        end if;
1258
 
1259
      -- Mnemonic MOV_T -------------------------------------------------------
1260
      when MN_MOV_T =>
1261
        if clk_mstate_i = MSTATE3 then
1262
          if opc_opcode_s(5) = '1' then
1263
            alu_read_alu_o    <= true;  -- MOV T, A
1264
            tim_write_timer_o <= true;
1265
          else
1266
            tim_read_timer_o  <= true;  -- MOV A, T
1267
            alu_write_accu_o  <= true;
1268
          end if;
1269
        end if;
1270
 
1271 21 arniml
      -- Mnemonic OUTD_PP_A ---------------------------------------------------
1272
      when MN_OUTD_PP_A =>
1273
        clk_assert_prog_o     <= true;
1274
 
1275
        if not clk_second_cycle_i then
1276
          case clk_mstate_i is
1277
            -- propagate expander port number to Port 2
1278
            when MSTATE3 =>
1279
 
1280
              data_s(7 downto 4)     <= (others => '0');
1281
              data_s(1 downto 0)     <= opc_opcode_s(1 downto 0);
1282
              -- decide which 8243 command to use
1283
              case opc_opcode_s(7 downto 4) is
1284
                when "1001" =>
1285
                  data_s(3 downto 2) <= "11";  -- ANLD command
1286
                when "1000" =>
1287
                  data_s(3 downto 2) <= "10";  -- ORLD command
1288
                when "0011" =>
1289
                  data_s(3 downto 2) <= "01";  -- MOVD command
1290
                when others =>
1291
                  null;
1292
              end case;
1293
 
1294
              read_dec_s      <= true;
1295
              p2_write_exp_o  <= true;
1296
 
1297
            -- output expander port number on Port 2 while active edge of PROG
1298
            -- write Accumulator to expander port
1299
            when MSTATE4 =>
1300
              p2_output_exp_o <= true;
1301
 
1302
              alu_read_alu_o  <= true;
1303
              p2_write_exp_o  <= true;
1304
 
1305
            when MSTATE5 =>
1306
              p2_output_exp_o <= true;
1307
 
1308
            when others =>
1309
              null;
1310
 
1311
          end case;
1312
 
1313
        else
1314
          -- hold expander port until inactive edge of PROG 
1315
          if clk_mstate_i = MSTATE1 or clk_mstate_i = MSTATE2 then
1316
            p2_output_exp_o   <= true;
1317
          end if;
1318
 
1319
        end if;
1320
 
1321
      -- Mnemonic MOVD_A_PP ---------------------------------------------------
1322
      when MN_MOVD_A_PP =>
1323
        clk_assert_prog_o            <= true;
1324
 
1325
        if not clk_second_cycle_i then
1326
          case clk_mstate_i is
1327
            -- propagate expander port number to Port 2
1328
            when MSTATE3 =>
1329
              data_s                 <= "0000" &
1330
                                        "00"   &  -- 8243 command: read
1331
                                        opc_opcode_s(1 downto 0);
1332
              read_dec_s             <= true;
1333
              p2_write_exp_o         <= true;
1334
 
1335
            -- output expander port number on Port 2 while active edge of PROG
1336
            -- write 1's to expander port to set lower nibble of Port 2 to input
1337
            when MSTATE4 =>
1338
              p2_output_exp_o        <= true;
1339
 
1340
              data_s(nibble_t'range) <= (others => '1');
1341
              read_dec_s             <= true;
1342
              p2_write_exp_o         <= true;
1343
 
1344
            when MSTATE5 =>
1345
              p2_output_exp_o        <= true;
1346
 
1347
            when others =>
1348
              null;
1349
 
1350
          end case;
1351
 
1352
        else
1353
          case clk_mstate_i is
1354
            -- hold expander port until inactive edge of PROG
1355
            when MSTATE1 =>
1356
              p2_output_exp_o  <= true;
1357
 
1358
            -- hold expander port until inactive edge of PROG
1359
            -- write Accumulator with nibble of expander port
1360
            when MSTATE2 =>
1361
              p2_output_exp_o  <= true;
1362
              p2_read_exp_o    <= true;
1363
              alu_write_accu_o <= true;
1364
 
1365
            when others =>
1366
              null;
1367
 
1368
          end case;
1369
 
1370
        end if;
1371
 
1372 4 arniml
      -- Mnemonic MOVP --------------------------------------------------------
1373
      when MN_MOVP =>
1374
        assert_psen_s        <= true;
1375
 
1376
        if not clk_second_cycle_i then
1377
          -- write Accumulator to Program Memory address
1378
          -- (skip page offset update from Program Counter)
1379
          if clk_mstate_i = MSTATE3 then
1380
            alu_read_alu_o   <= true;
1381
            if opc_opcode_s(6) = '0' then
1382
              pm_addr_type_o <= PM_PAGE;
1383
            else
1384
              pm_addr_type_o <= PM_PAGE3;
1385
            end if;
1386
          end if;
1387
 
1388
        else
1389
          if clk_mstate_i = MSTATE1 then
1390
            -- store data from Program Memory in Accumulator
1391
            alu_write_accu_o <= true;
1392
            -- trick & treat to prevent additional PC increment
1393
            -- our branch target is the previously incremented PC!
1394
            branch_taken_s   <= true;
1395
          end if;
1396
 
1397
        end if;
1398
 
1399
      -- Mnemonic MOVX --------------------------------------------------------
1400
      when MN_MOVX =>
1401
        bus_bidir_bus_o        <= true;
1402
 
1403
        if opc_opcode_s(4) = '0' then
1404
          clk_assert_rd_o      <= true;
1405
        else
1406
          clk_assert_wr_o      <= true;
1407
        end if;
1408
 
1409
        if not clk_second_cycle_i then
1410
          -- read dmem and put contents on BUS as external address
1411
          if clk_mstate_i = MSTATE3 then
1412
            dm_read_dmem_o     <= true;
1413
            bus_write_bus_o    <= true;
1414
          end if;
1415
 
1416
        else
1417
          if clk_mstate_i = MSTATE1 then
1418
            if opc_opcode_s(4) = '0' then
1419
              -- store contents of BUS in Accumulator
1420
              add_read_bus_s   <= true;
1421
              alu_write_accu_o <= true;
1422
            else
1423
              -- store contents of Accumulator to BUS
1424
              alu_read_alu_o   <= true;
1425
              bus_write_bus_o  <= true;
1426
            end if;
1427
          end if;
1428
 
1429
        end if;
1430
 
1431
      -- Mnemonic NOP ---------------------------------------------------------
1432
      when MN_NOP =>
1433
        -- nothing to do
1434
 
1435
      -- Mnemonic ORL ---------------------------------------------------------
1436
      when MN_ORL =>
1437
        case clk_mstate_i is
1438
          -- read RAM once for indirect address mode
1439
          when MSTATE3 =>
1440
            address_indirect_3_f;
1441
 
1442
          -- store data from RAM to Temp Reg
1443
          when MSTATE4 =>
1444
            and_or_xor_add_4_f;
1445
 
1446
          -- perform OR and store in Accumulator
1447
          when MSTATE5 =>
1448
            and_or_xor_add_5_f(alu_op => ALU_OR);
1449
 
1450
          when others =>
1451
            null;
1452
 
1453
        end case;
1454
 
1455
      -- Mnemonic ORL_A_DATA --------------------------------------------------
1456
      when MN_ORL_A_DATA =>
1457
        assert_psen_s              <= true;
1458
 
1459
        if clk_second_cycle_i then
1460
          case clk_mstate_i is
1461
            -- write Temp Reg when contents of Program Memory is on bus
1462
            when MSTATE1 =>
1463
              alu_write_temp_reg_o <= true;
1464
 
1465
            -- perform OR and store in Accumulator
1466
            when MSTATE3 =>
1467
              and_or_xor_add_5_f(alu_op => ALU_OR);
1468
 
1469
            when others =>
1470
              null;
1471
 
1472
          end case;
1473
 
1474
        end if;
1475
 
1476
      -- Mnemonic ORL_EXT -----------------------------------------------------
1477
      when MN_ORL_EXT =>
1478
        assert_psen_s            <= true;
1479
 
1480
        if not clk_second_cycle_i then
1481
          -- read port to Temp Reg
1482
          if clk_mstate_i = MSTATE5 then
1483
            if opc_opcode_s(1 downto 0) = "00" then
1484
              add_read_bus_s     <= true;
1485
            elsif opc_opcode_s(1) = '0' then
1486
              p1_read_p1_o       <= true;
1487
              p1_read_reg_o      <= true;
1488
            else
1489
              p2_read_p2_o       <= true;
1490
              p2_read_reg_o      <= true;
1491
            end if;
1492
 
1493
            alu_write_temp_reg_o <= true;
1494
          end if;
1495
 
1496
        else
1497
          case clk_mstate_i is
1498
            -- write shadow Accumulator when contents of Program Memory is
1499
            -- on bus
1500
            when MSTATE1 =>
1501
              alu_write_shadow_o <= true;
1502
 
1503
            -- loop shadow Accumulator through ALU to prevent update from
1504
            -- real Accumulator
1505
            when MSTATE2 =>
1506
              alu_read_alu_o     <= true;
1507
              alu_write_shadow_o <= true;
1508
 
1509
            -- write result of OR operation back to port
1510
            when MSTATE3 =>
1511
              alu_op_o           <= ALU_OR;
1512
              alu_read_alu_o     <= true;
1513
 
1514
              if opc_opcode_s(1 downto 0) = "00" then
1515
                bus_write_bus_o  <= true;
1516
              elsif opc_opcode_s(1) = '0' then
1517
                p1_write_p1_o    <= true;
1518
              else
1519
                p2_write_p2_o    <= true;
1520
              end if;
1521
 
1522
            when others =>
1523
              null;
1524
 
1525
          end case;
1526
 
1527
        end if;
1528
 
1529
      -- Mnemonic OUTL_EXT ----------------------------------------------------
1530
      when MN_OUTL_EXT =>
1531
        -- read Accumulator and store in Port/BUS output register
1532
        if clk_second_cycle_i and clk_mstate_i = MSTATE4 then
1533
          alu_read_alu_o  <= true;
1534
 
1535
          if opc_opcode_s(4) = '1' then
1536
            if opc_opcode_s(1) = '0' then
1537
              p1_write_p1_o <= true;
1538
            else
1539
              p2_write_p2_o <= true;
1540
            end if;
1541
 
1542
          else
1543
            bus_write_bus_o <= true;
1544
 
1545
          end if;
1546
 
1547
        end if;
1548
 
1549
      -- Mnemonic RET ---------------------------------------------------------
1550
      when MN_RET =>
1551
        if not clk_second_cycle_i then
1552
          case clk_mstate_i is
1553
            -- decrement Stack Pointer
1554
            when MSTATE3 =>
1555
              psw_dec_stackp_o     <= true;
1556
 
1557
            -- read Stack Pointer and address Data Memory for low byte
1558
            when MSTATE4 =>
1559
              psw_read_sp_o        <= true;
1560
              dm_write_dmem_addr_o <= true;
1561
              dm_addr_type_o       <= DM_STACK;
1562
 
1563
            -- read Data Memory and store to Program Counter low
1564
            -- prepare address to Data memory for high byte
1565
            when MSTATE5 =>
1566
              dm_read_dmem_o       <= true;
1567
              pm_write_pcl_o       <= true;
1568
              dm_write_dmem_addr_o <= true;
1569
              dm_addr_type_o       <= DM_STACK_HIGH;
1570
 
1571
            when others =>
1572
              null;
1573
 
1574
          end case;
1575
 
1576
        else
1577
          case clk_mstate_i is
1578
            -- read Data Memory and store to Program Counter high and PSW
1579
            when MSTATE1 =>
1580
              dm_read_dmem_o         <= true;
1581
              pm_write_pch_o         <= true;
1582
              if opc_opcode_s(4) = '1' then
1583
                psw_write_psw_o      <= true;
1584
                retr_executed_s      <= true;
1585
              end if;
1586
 
1587
            when MSTATE2 =>
1588
              add_write_pmem_addr_s  <= true;
1589
 
1590
            when others =>
1591
              null;
1592
 
1593
          end case;
1594
 
1595
        end if;
1596
 
1597
      -- Mnemonic RL ----------------------------------------------------------
1598
      when MN_RL =>
1599
        if clk_mstate_i = MSTATE3 then
1600
          alu_op_o             <= ALU_RL;
1601
          alu_read_alu_o       <= true;
1602
          alu_write_accu_o     <= true;
1603
 
1604
          if opc_opcode_s(4) = '1' then
1605
            psw_special_data_o <= alu_carry_i;
1606
            psw_write_carry_o  <= true;
1607
            alu_use_carry_o    <= true;
1608
          end if;
1609
        end if;
1610
 
1611
      -- Mnemonic RR ----------------------------------------------------------
1612
      when MN_RR =>
1613
        if clk_mstate_i = MSTATE3 then
1614
          alu_op_o             <= ALU_RR;
1615
          alu_read_alu_o       <= true;
1616
          alu_write_accu_o     <= true;
1617
 
1618
          if opc_opcode_s(4) = '0' then
1619
            psw_special_data_o <= alu_carry_i;
1620
            psw_write_carry_o  <= true;
1621
            alu_use_carry_o    <= true;
1622
          end if;
1623
        end if;
1624
 
1625
      -- Mnemonic SEL_MB ------------------------------------------------------
1626
      when MN_SEL_MB =>
1627
        if clk_mstate_i = MSTATE3 then
1628
          if opc_opcode_s(4) = '1' then
1629
            set_mb_s   <= true;
1630
          else
1631
            clear_mb_s <= true;
1632
          end if;
1633
        end if;
1634
 
1635
      -- Mnemonic SEL_RB ------------------------------------------------------
1636
      when MN_SEL_RB =>
1637
        if clk_mstate_i = MSTATE3 then
1638
          psw_special_data_o <= opc_opcode_s(4);
1639
          psw_write_bs_o     <= true;
1640
        end if;
1641
 
1642
      -- Mnemonic STOP_TCNT ---------------------------------------------------
1643
      when MN_STOP_TCNT =>
1644
        if clk_mstate_i = MSTATE3 then
1645
          tim_stop_tcnt_o <= true;
1646
        end if;
1647
 
1648
      -- Mnemonic STRT --------------------------------------------------------
1649
      when MN_STRT =>
1650
        if clk_mstate_i = MSTATE3 then
1651
          if opc_opcode_s(4) = '1' then
1652
            tim_start_t_o   <= true;
1653
          else
1654
            tim_start_cnt_o <= true;
1655
          end if;
1656
        end if;
1657
 
1658
      -- Mnemonic SWAP --------------------------------------------------------
1659
      when MN_SWAP =>
1660
        alu_op_o           <= ALU_SWAP;
1661
 
1662
        if clk_mstate_i = MSTATE3 then
1663
          alu_read_alu_o   <= true;
1664
          alu_write_accu_o <= true;
1665
        end if;
1666
 
1667
      -- Mnemonic XCH ---------------------------------------------------------
1668
      when MN_XCH =>
1669
        case clk_mstate_i is
1670
          -- read RAM once for indirect address mode
1671
          when MSTATE3 =>
1672
            address_indirect_3_f;
1673
 
1674 38 arniml
          -- store data from RAM in Accumulator and Temp Reg
1675 4 arniml
          -- Accumulator is already shadowed!
1676
          when MSTATE4 =>
1677 38 arniml
            dm_read_dmem_o       <= true;
1678
            alu_write_accu_o     <= true;
1679
            alu_write_temp_reg_o <= true;
1680
            if opc_opcode_s(4) = '1' then
1681
              -- XCHD
1682
              -- only write lower nibble of Accumulator
1683
              alu_accu_low_o     <= true;
1684
            end if;
1685 4 arniml
 
1686
          -- store data from shadow (previous) Accumulator to dmem
1687
          when MSTATE5 =>
1688 38 arniml
            dm_write_dmem_s      <= true;
1689
            alu_read_alu_o       <= true;
1690
            if opc_opcode_s(4) = '1' then
1691
              -- XCHD
1692
              -- concatenate shadow Accumulator and Temp Reg
1693
              alu_op_o           <= ALU_CONCAT;
1694
            end if;
1695 4 arniml
 
1696
          when others =>
1697
            null;
1698
 
1699
        end case;
1700
 
1701
      -- Mnemonic XRL ---------------------------------------------------------
1702
      when MN_XRL =>
1703
        case clk_mstate_i is
1704
          -- read RAM once for indirect address mode
1705
          when MSTATE3 =>
1706
            address_indirect_3_f;
1707
 
1708
          -- store data from RAM to Temp Reg
1709
          when MSTATE4 =>
1710
            and_or_xor_add_4_f;
1711
 
1712
          -- perform XOR and store in Accumulator
1713
          when MSTATE5 =>
1714
            and_or_xor_add_5_f(alu_op => ALU_XOR);
1715
 
1716
          when others =>
1717
            null;
1718
 
1719
        end case;
1720
 
1721
      -- Mnemonic XRL_A_DATA --------------------------------------------------
1722
      when MN_XRL_A_DATA =>
1723
        assert_psen_s              <= true;
1724
 
1725
        if clk_second_cycle_i then
1726
          case clk_mstate_i is
1727
            -- write Temp Reg when contents of Program Memory is on bus
1728
            when MSTATE1 =>
1729
              alu_write_temp_reg_o <= true;
1730
 
1731
            -- perform XOR and store in Accumulator
1732
            when MSTATE3 =>
1733
              and_or_xor_add_5_f(alu_op => ALU_XOR);
1734
 
1735
            when others =>
1736
              null;
1737
 
1738
          end case;
1739
 
1740
        end if;
1741
 
1742
      -- Unimplemented mnemonic -----------------------------------------------
1743
      when others =>
1744
        -- this will behave like a NOP
1745
 
1746
        -- pragma translate_off
1747
        assert false
1748
          report "Mnemonic not yet implemented."
1749
          severity warning;
1750
        -- pragma translate_on
1751
 
1752
    end case;
1753
 
1754
  end process decode;
1755
  --
1756
  -----------------------------------------------------------------------------
1757
 
1758
 
1759
  -----------------------------------------------------------------------------
1760
  -- Process regs
1761
  --
1762
  -- Purpose:
1763
  --   Implements the various registes.
1764
  --
1765
  regs: process (res_i, clk_i)
1766
  begin
1767
    if res_i = res_active_c then
1768
      branch_taken_q <= false;
1769
      f1_q           <= '0';
1770
      mb_q           <= '0';
1771
      t0_dir_q       <= '0';
1772 38 arniml
      -- pragma translate_off
1773
      istrobe_s      <= '0';
1774
      -- pragma translate_on
1775 4 arniml
 
1776
    elsif clk_i'event and clk_i = clk_active_c then
1777
      if en_clk_i then
1778
 
1779
        -- branch taken flag
1780
        if branch_taken_s then
1781
          branch_taken_q <= true;
1782
        elsif clk_mstate_i = MSTATE5 then
1783
          -- release flag when new instruction starts
1784
          branch_taken_q <= false;
1785
        end if;
1786
 
1787
        -- Flag 1
1788
        if clear_f1_s then
1789
          f1_q         <= '0';
1790
        elsif cpl_f1_s then
1791
          f1_q         <= not f1_q;
1792
        end if;
1793
 
1794
        -- Memory Bank select
1795
        if clear_mb_s then
1796
          mb_q         <= '0';
1797
        elsif set_mb_s then
1798
          mb_q         <= '1';
1799
        end if;
1800
 
1801
        -- T0 direction selection
1802
        if ent0_clk_s then
1803
          t0_dir_q     <= '1';
1804
        end if;
1805
 
1806
      end if;
1807
 
1808 38 arniml
      -- pragma translate_off
1809
      -- Instruction Strobe ---------------------------------------------------
1810
      if clk_mstate_i = MSTATE5 and last_cycle_s then
1811
        istrobe_s      <= '1';
1812
      else
1813
        istrobe_s      <= '0';
1814
      end if;
1815
      -- pragma translate_on
1816
 
1817 4 arniml
    end if;
1818
 
1819
  end process regs;
1820
  --
1821
  -----------------------------------------------------------------------------
1822
 
1823
 
1824
  -----------------------------------------------------------------------------
1825
  -- Output Mapping.
1826
  -----------------------------------------------------------------------------
1827
  clk_multi_cycle_o    <= opc_multi_cycle_s;
1828
  cnd_f1_o             <= f1_q;
1829
  cnd_tf_o             <= tf_s;
1830
  data_o               <=   data_s
1831
                          when read_dec_s else
1832
                            (others => bus_idle_level_c);
1833
  dm_write_dmem_o      <= dm_write_dmem_s      and en_clk_i;
1834
  pm_inc_pc_o          <= pm_inc_pc_s          or add_inc_pc_s;
1835
  pm_write_pmem_addr_o <= pm_write_pmem_addr_s or add_write_pmem_addr_s;
1836
  t0_dir_o             <= t0_dir_q;
1837
  bus_read_bus_o       <= bus_read_bus_s       or add_read_bus_s;
1838
 
1839
end rtl;
1840
 
1841
 
1842
-------------------------------------------------------------------------------
1843
-- File History:
1844
--
1845
-- $Log: not supported by cvs2svn $
1846 38 arniml
-- Revision 1.3  2004/03/28 21:15:48  arniml
1847
-- implemented mnemonic DA
1848
--
1849 27 arniml
-- Revision 1.2  2004/03/28 13:06:32  arniml
1850
-- implement mnemonics:
1851
--    + MOVD_A_PP
1852
--    + OUTD_PP_A -> ANLD PP, A; MOVD PP, A; ORLD PP, A
1853
--
1854 21 arniml
-- Revision 1.1  2004/03/23 21:31:52  arniml
1855
-- initial check-in
1856 27 arniml
--
1857 4 arniml
-------------------------------------------------------------------------------

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.