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

Subversion Repositories eco32

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 308 hellwig
//
2
// tmr.v -- programmable timer
3
//
4
 
5
 
6
`timescale 1ns/10ps
7
`default_nettype none
8
 
9
 
10
module tmr(clk, rst,
11
           stb, we, addr,
12
           data_in, data_out,
13
           ack, irq);
14
    input clk;
15
    input rst;
16
    input stb;
17
    input we;
18
    input [3:2] addr;
19
    input [31:0] data_in;
20
    output reg [31:0] data_out;
21
    output ack;
22
    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
    if (divisor_loaded) begin
33
      counter <= divisor;
34
      expired <= 0;
35
    end else begin
36
      if (counter == 32'h00000001) begin
37
        counter <= divisor;
38
        expired <= 1;
39
      end else begin
40
        counter <= counter - 1;
41
        expired <= 0;
42
      end
43
    end
44
  end
45
 
46
  always @(posedge clk) begin
47
    if (rst) begin
48
      divisor <= 32'hFFFFFFFF;
49
      divisor_loaded <= 1;
50
      alarm <= 0;
51
      ien <= 0;
52
    end else begin
53
      if (expired) begin
54
        alarm <= 1;
55
      end else begin
56
        if (stb == 1 && we == 1 && addr[3:2] == 2'b00) begin
57
          // ctrl
58
          alarm <= data_in[0];
59
          ien <= data_in[1];
60
        end
61
        if (stb == 1 && we == 1 && addr[3:2] == 2'b01) begin
62
          // divisor
63
          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
  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
  assign ack = stb;
92
  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.