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

Subversion Repositories eco32

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

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 27 hellwig
module tmr(clk, reset,
7 67 hellwig
           en, wr, addr,
8 27 hellwig
           data_in, data_out,
9
           wt, irq);
10
    input clk;
11
    input reset;
12
    input en;
13
    input wr;
14 67 hellwig
    input addr;
15 27 hellwig
    input [31:0] data_in;
16
    output [31:0] data_out;
17
    output wt;
18
    output irq;
19
 
20 68 hellwig
  reg [5:0] prescaler;
21 27 hellwig
  reg tick;
22
  reg [31:0] counter;
23
  reg [31:0] divisor;
24
  reg divisor_loaded;
25
  reg expired;
26
  reg alarm;
27
  reg ien;
28
 
29
  always @(posedge clk) begin
30
    if (reset == 1) begin
31 68 hellwig
      prescaler <= 6'd50;
32 27 hellwig
      tick <= 0;
33
    end else begin
34 68 hellwig
      if (prescaler == 6'd1) begin
35
        prescaler <= 6'd50;
36 27 hellwig
        tick <= 1;
37
      end else begin
38
        prescaler <= prescaler - 1;
39
        tick <= 0;
40
      end
41
    end
42
  end
43
 
44
  always @(posedge clk) begin
45
    if (divisor_loaded == 1) begin
46
      counter <= divisor;
47
      expired <= 0;
48
    end else begin
49
      if (tick == 1) begin
50
        if (counter == 32'h00000001) begin
51
          counter <= divisor;
52
          expired <= 1;
53
        end else begin
54
          counter <= counter - 1;
55
          expired <= 0;
56
        end
57
      end else begin
58
        expired <= 0;
59
      end
60
    end
61
  end
62
 
63
  always @(posedge clk) begin
64
    if (reset == 1) begin
65
      divisor <= 32'hFFFFFFFF;
66
      divisor_loaded <= 1;
67
      alarm <= 0;
68
      ien <= 0;
69
    end else begin
70
      if (expired == 1) begin
71
        alarm <= 1;
72
      end else begin
73 67 hellwig
        if (en == 1 && wr == 1 && addr == 0) begin
74 27 hellwig
          alarm <= data_in[0];
75
          ien <= data_in[1];
76
        end
77 67 hellwig
        if (en == 1 && wr == 1 && addr == 1) begin
78 27 hellwig
          divisor <= data_in;
79
          divisor_loaded <= 1;
80
        end else begin
81
          divisor_loaded <= 0;
82
        end
83
      end
84
    end
85
  end
86
 
87
  assign data_out =
88 67 hellwig
    (addr == 0) ? { 28'h0000000, 2'b00, ien, alarm } :
89 27 hellwig
                   divisor;
90
  assign wt = 0;
91
  assign irq = ien & alarm;
92
 
93
endmodule

powered by: WebSVN 2.1.0

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