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

Subversion Repositories eco32

[/] [eco32/] [trunk/] [fpga/] [mc/] [src/] [tmr/] [tmr.v] - Blame information for rev 314

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

Line No. Rev Author Line
1 68 hellwig
//
2
// tmr.v -- programmable timer
3
//
4
 
5
 
6 290 hellwig
`timescale 1ns/10ps
7
`default_nettype none
8
 
9
 
10
module tmr(clk, rst,
11
           stb, we, addr,
12 27 hellwig
           data_in, data_out,
13 290 hellwig
           ack, irq);
14 27 hellwig
    input clk;
15 290 hellwig
    input rst;
16
    input stb;
17
    input we;
18 69 hellwig
    input [3:2] addr;
19 27 hellwig
    input [31:0] data_in;
20 69 hellwig
    output reg [31:0] data_out;
21 290 hellwig
    output ack;
22 27 hellwig
    output irq;
23
 
24
  reg [31:0] counter;
25
  reg [31:0] divisor;
26
  reg divisor_loaded;
27
  reg expired;
28
  reg alarm;
29
  reg ien;
30
 
31
  always @(posedge clk) begin
32 290 hellwig
    if (divisor_loaded) begin
33 27 hellwig
      counter <= divisor;
34
      expired <= 0;
35
    end else begin
36 69 hellwig
      if (counter == 32'h00000001) begin
37
        counter <= divisor;
38
        expired <= 1;
39 27 hellwig
      end else begin
40 69 hellwig
        counter <= counter - 1;
41 27 hellwig
        expired <= 0;
42
      end
43
    end
44
  end
45
 
46
  always @(posedge clk) begin
47 290 hellwig
    if (rst) begin
48 27 hellwig
      divisor <= 32'hFFFFFFFF;
49
      divisor_loaded <= 1;
50
      alarm <= 0;
51
      ien <= 0;
52
    end else begin
53 290 hellwig
      if (expired) begin
54 27 hellwig
        alarm <= 1;
55
      end else begin
56 290 hellwig
        if (stb == 1 && we == 1 && addr[3:2] == 2'b00) begin
57
          // ctrl
58 27 hellwig
          alarm <= data_in[0];
59
          ien <= data_in[1];
60
        end
61 290 hellwig
        if (stb == 1 && we == 1 && addr[3:2] == 2'b01) begin
62
          // divisor
63 27 hellwig
          divisor <= data_in;
64
          divisor_loaded <= 1;
65
        end else begin
66
          divisor_loaded <= 0;
67
        end
68
      end
69
    end
70
  end
71
 
72 69 hellwig
  always @(*) begin
73
    case (addr[3:2])
74
      2'b00:
75
        // ctrl
76
        data_out = { 28'h0000000, 2'b00, ien, alarm };
77
      2'b01:
78
        // divisor
79
        data_out = divisor;
80
      2'b10:
81
        // counter
82
        data_out = counter;
83
      2'b11:
84
        // not used
85
        data_out = 32'hxxxxxxxx;
86
      default:
87
        data_out = 32'hxxxxxxxx;
88
    endcase
89
  end
90
 
91 290 hellwig
  assign ack = stb;
92 27 hellwig
  assign irq = ien & alarm;
93
 
94
endmodule

powered by: WebSVN 2.1.0

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