1 /*
2    Copyright (C) 1998,1999,2000 T. Scott Dattalo
3 
4 This file is part of the libgpsim library of gpsim
5 
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10 
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 Lesser General Public License for more details.
15 
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, see
18 <http://www.gnu.org/licenses/lgpl-2.1.html>.
19 */
20 
21 #ifndef SRC_TMR0_H_
22 #define SRC_TMR0_H_
23 
24 #include <glib.h>
25 
26 #include "gpsim_classes.h"
27 #include "registers.h"
28 #include "stimuli.h"
29 #include "trigger.h"
30 
31 #include "ioports.h"
32 
33 
34 class TMR0_Interface;
35 class T1GCON;
36 class ADCON2_TRIG;
37 class CLC_BASE;
38 class OPTION_REG;
39 class PinModule;
40 class PortRegister;
41 class Processor;
42 
43 
44 //---------------------------------------------------------
45 // TMR0 - Timer
46 class TMR0 : public sfr_register, public TriggerObject, public SignalSink, public apfpin
47 {
48 public:
49     TMR0(Processor *, const char *pName, const char *pDesc = nullptr);
50 
51     virtual void callback();
52     virtual void release();
53 
54     virtual void put(unsigned int new_value);
55     virtual void put_value(unsigned int new_value);
56     virtual unsigned int get();
57     virtual unsigned int get_value();
58     virtual void start(int new_value, int sync = 0);
59     virtual void stop();
60     virtual void increment();   // Used when tmr0 is attached to an external clock
61     virtual void new_prescale();
62     virtual unsigned int get_prescale();
max_counts()63     virtual unsigned int max_counts() { return 256; }
64     virtual bool get_t0cs();
65     virtual bool get_t0se();
66     virtual void set_t0if();
set_t0xcs(bool _t0xcs)67     virtual void set_t0xcs(bool _t0xcs) { t0xcs = _t0xcs; }
get_t0xcs()68     virtual bool get_t0xcs() { return t0xcs; }
69     virtual void reset(RESET_TYPE r);
70     virtual void callback_print();
71     virtual void clear_trigger();
72 
73     virtual void set_cpu(Processor *, PortRegister *, unsigned int pin, OPTION_REG *);
74     virtual void set_cpu(Processor *new_cpu, PinModule *pin, OPTION_REG *);
75     virtual void setIOpin(PinModule * pin, int arg=0);
76 
77     virtual void setSinkState(char);
78     virtual void sleep();
79     virtual void wake();
set_t1gcon(T1GCON * _t1gcon)80     void set_t1gcon(T1GCON *_t1gcon) { m_t1gcon = _t1gcon; }
set_adcon2(ADCON2_TRIG * _adcon2)81     void set_adcon2(ADCON2_TRIG *_adcon2) { m_adcon2 = _adcon2; }
set_clc(CLC_BASE * _clc,int index)82     void set_clc(CLC_BASE *_clc, int index) { m_clc[index] = _clc; }
83 
84     enum
85     {
86         STOPPED = 0,
87         RUNNING = 1,
88         SLEEPING = 2
89     };
90     unsigned int prescale;
91     unsigned int prescale_counter = 0;
92     unsigned int old_option = 0;       // Save option register contents here.
93     unsigned int state;            // Either on or off right now.
94 
95     guint64 synchronized_cycle = 0;
96     guint64 future_cycle = 0;
97     gint64 last_cycle = 0;   // can be negative ...
98 
99     OPTION_REG *m_pOptionReg = nullptr;
100 
101 protected:
102     T1GCON 	*m_t1gcon = nullptr;
103     ADCON2_TRIG   *m_adcon2 = nullptr;
104     CLC_BASE	*m_clc[4];
105 
106 private:
107     bool 		m_bLastClockedState = false;
108     PinModule     *pin = nullptr;
109     bool		t0xcs = false;	//  clock source is the capacitive sensing oscillator
110     TMR0_Interface *tmr0_interface = nullptr;
111 };
112 
113 #endif
114