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

Subversion Repositories tv80

[/] [tv80/] [trunk/] [env/] [env_io.v] - Blame information for rev 102

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

Line No. Rev Author Line
1 2 ghutchis
 
2
module env_io (/*AUTOARG*/
3 89 ghutchis
  // Inouts
4
  DI,
5 2 ghutchis
  // Inputs
6 89 ghutchis
  clk, iorq_n, rd_n, wr_n, addr, D_OUT
7 2 ghutchis
  );
8
 
9
  input clk;
10
  input iorq_n;
11
  input rd_n;
12
  input wr_n;
13
  input [7:0] addr;
14 89 ghutchis
  input [7:0] D_OUT;
15 2 ghutchis
  inout [7:0] DI;
16
 
17
  reg [7:0]    io_data;
18
 
19
  reg [7:0]    str_buf [0:255];
20
  reg          io_cs;
21
  integer      buf_ptr, i;
22
 
23
  reg [7:0]    timeout_ctl;
24
  reg [15:0]   cur_timeout;
25
  reg [15:0]   max_timeout;
26
 
27
  reg [7:0]    int_countdown;
28 89 ghutchis
  reg [7:0]    nmi_countdown;
29 37 ghutchis
  reg [7:0]    checksum;
30
  reg [7:0]    ior_value;  // increment-on-read value
31 89 ghutchis
  reg [7:0]    nmi_trigger; // trigger nmi when IR = this value
32 2 ghutchis
 
33
  assign       DI = (!iorq_n & !rd_n & io_cs) ? io_data : {8{1'bz}};
34
 
35
  initial
36
    begin
37
      io_cs = 0;
38
      buf_ptr = 0;
39
      cur_timeout = 0;
40
      max_timeout = 10000;
41
      timeout_ctl = 1;
42
      int_countdown = 0;
43 89 ghutchis
      nmi_countdown = 0;
44
      nmi_trigger = 0;
45 2 ghutchis
    end
46 37 ghutchis
 
47
  always @*
48
    begin
49
      if (!iorq_n & !rd_n)
50
        begin
51
          io_cs = (addr[7:5] == 3'b100);
52
 
53
          case (addr)
54 53 ghutchis
            8'h82 : io_data = timeout_ctl;
55 37 ghutchis
            8'h83 : io_data = max_timeout[7:0];
56
            8'h84 : io_data = max_timeout[15:8];
57
 
58
            8'h90 : io_data = int_countdown;
59
            8'h91 : io_data = checksum;
60
            8'h93 : io_data = ior_value;
61 41 ghutchis
            8'h94 : io_data = {$random};
62 89 ghutchis
            8'h95 : io_data = nmi_countdown[7:0];
63
            8'hA0 : io_data = nmi_trigger;
64 37 ghutchis
            default : io_data = 8'hzz;
65
          endcase // case(addr)
66
        end // if (!iorq_n & !rd_n)
67
    end // always @ *
68 75 ghutchis
 
69
  wire wr_stb;
70
  reg last_iowrite;
71
 
72
  assign wr_stb = (!iorq_n & !wr_n);
73
 
74 2 ghutchis
  always @(posedge clk)
75
    begin
76 75 ghutchis
      last_iowrite <= #1 wr_stb;
77
      if (!wr_stb & last_iowrite)
78 2 ghutchis
        case (addr)
79
          8'h80 :
80
            begin
81 89 ghutchis
              case (D_OUT)
82
                1 :
83
                  begin
84
                    $writememh ("test_output2.hex", tb_top.rom.mem);
85
                    tb_top.test_pass;
86
                  end
87 2 ghutchis
 
88 89 ghutchis
                2 :
89
                  begin
90
                    $writememh ("test_output2.hex", tb_top.rom.mem);
91
                    tb_top.test_fail;
92
                  end
93 2 ghutchis
 
94
                3 : tb_top.dumpon;
95
 
96
                4 : tb_top.dumpoff;
97
 
98
                default :
99
                  begin
100 89 ghutchis
                    $display ("%t: ERROR   : Unknown I/O command %x", $time, D_OUT);
101 2 ghutchis
                  end
102 89 ghutchis
              endcase // case(D_OUT)
103 2 ghutchis
            end // case: :...
104
 
105
          8'h81 :
106
            begin
107 89 ghutchis
              str_buf[buf_ptr] = D_OUT;
108 2 ghutchis
              buf_ptr = buf_ptr + 1;
109
 
110 89 ghutchis
              //$display ("%t: DEBUG   : Detected write of character %x", $time, D_OUT);
111
              if (D_OUT == 8'h0A)
112 2 ghutchis
                begin
113
                  $write ("%t: PROGRAM : ", $time);
114
 
115
                  for (i=0; i<buf_ptr; i=i+1)
116
                    $write ("%s", str_buf[i]);
117
 
118
                  buf_ptr = 0;
119
                end
120
            end // case: 8'h81
121
 
122
          8'h82 :
123
            begin
124 89 ghutchis
              timeout_ctl = D_OUT;
125 2 ghutchis
            end
126
 
127 89 ghutchis
          8'h83 : max_timeout[7:0] = D_OUT;
128
          8'h84 : max_timeout[15:8] = D_OUT;
129 2 ghutchis
 
130 89 ghutchis
          8'h90 : int_countdown = D_OUT;
131
          8'h91 : checksum = D_OUT;
132
          8'h92 : checksum = checksum + D_OUT;
133
          8'h93 : ior_value = D_OUT;
134
          8'h95 : nmi_countdown[7:0] = D_OUT;
135
          8'hA0 : nmi_trigger = D_OUT;
136 2 ghutchis
        endcase // case(addr)
137
    end // always @ (posedge clk)
138
 
139
  always @(posedge clk)
140
    begin
141
      if (timeout_ctl[1])
142
        cur_timeout = 0;
143
      else if (timeout_ctl[0])
144
        cur_timeout = cur_timeout + 1;
145
 
146
      if (cur_timeout >= max_timeout)
147
        begin
148
          $display ("%t: ERROR   : Reached timeout %d cycles", $time, max_timeout);
149
          tb_top.test_fail;
150
        end
151
    end // always @ (posedge clk)
152
 
153
  always @(posedge clk)
154
    begin
155 89 ghutchis
      if (int_countdown == 0)
156
        begin
157
          tb_top.int_n  <= #1 1'b1;
158
        end
159
      else if (int_countdown == 1)
160 2 ghutchis
        begin
161
          tb_top.int_n  <= #1 1'b0;
162 89 ghutchis
          //int_countdown = 0;
163 2 ghutchis
        end
164
      else if (int_countdown > 1)
165 31 ghutchis
        begin
166
          int_countdown = int_countdown - 1;
167
          tb_top.int_n  <= #1 1'b1;
168
        end
169 89 ghutchis
 
170
      // when nmi countdown reaches 1, an NMI will be issued.
171
      // to clear the interrupt, write nmi_countdown to 0.
172
      if ((nmi_countdown == 0) && (nmi_trigger == 0))
173
        begin
174
          tb_top.nmi_n  <= #1 1'b1;
175
        end
176
      else if (nmi_countdown == 1)
177
        begin
178
          tb_top.nmi_n  <= #1 1'b0;
179
        end
180
      else if (nmi_countdown > 1)
181
        begin
182
          nmi_countdown = nmi_countdown - 1;
183
          tb_top.nmi_n  <= #1 1'b1;
184
        end
185
 
186
      // when IR equals the target instruction, an NMI will be
187
      // issued.  To clear the interrupt, write nmi_trigger to
188
      // zero.
189
      if (nmi_trigger != 0)
190
        begin
191
          if (nmi_trigger === tb_top.tv80s_inst.i_tv80_core.IR[7:0])
192
            begin
193
              tb_top.nmi_n <= #80 0;
194
              tb_top.nmi_n <= #160 1;
195
            end
196
        end
197
      else if (nmi_countdown == 0)
198
        tb_top.nmi_n <= #1 1;
199 2 ghutchis
    end
200
 
201
endmodule // env_io

powered by: WebSVN 2.1.0

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