-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathp18_tmr0.c
129 lines (106 loc) · 4.3 KB
/
p18_tmr0.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* ########################################################################
PICsim - PIC simulator
########################################################################
Copyright (c) : 2019-2020 Luis Claudio Gamboa Lopes
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
For e-mail suggestions : lcgamboa@yahoo.com
######################################################################## */
#include "../../include/periferic18.h"
#include "../../include/picsim.h"
#include <stdio.h>
extern const int fpw2[];
void p18_tmr0_rst(_pic *pic) { pic->cp0 = 0; }
void p18_tmr0(_pic *pic) {
if (pic->P18map.T0CON) {
if ((*pic->P18map.T0CON) & 0x80) // TMR0ON
{
if ((((*pic->P18map.T0CON) & 0x20) == 0x00) || // TOCS=FOSC/4
((((*pic->P18map.T0CON) & 0x30) == 0x20) &&
((pic->t0cki_ == 0) &&
(pic->pins[pic->t0cki - 1].value == 1))) || // T0CS=t0cki T0SE =0
((((*pic->P18map.T0CON) & 0x30) == 0x30) &&
((pic->t0cki_ == 1) &&
(pic->pins[pic->t0cki - 1].value == 0)))) // T0CS=t0cki T0SE =1
{
pic->cp0++;
if (pic->lram == sfr_addr(pic->P18map.TMR0L))
pic->cp0 = 0; // RESET prescaler
if (((*pic->P18map.T0CON) & 0x08) == 0x08) // PSA
{
if (((*pic->P18map.T0CON) & 0x40) &&
(((*pic->P18map.TMR0L) + 1) == 0x100))
(*pic->P18map.INTCON) |= 0x04; // T0IF
(*pic->P18map.TMR0L)++;
if ((!(*pic->P18map.TMR0L)) && (!((*pic->P18map.T0CON) & 0x40))) {
if (((*pic->P18map.TMR0H) + 1) == 0x100)
(*pic->P18map.INTCON) |= 0x04; // T0IF
(*pic->P18map.TMR0H)++;
}
pic->cp0 = 0;
} else {
if (pic->cp0 >= 2 * (fpw2[(*pic->P18map.T0CON) & 0x07])) {
if (((*pic->P18map.T0CON) & 0x40) &&
(((*pic->P18map.TMR0L) + 1) == 0x100))
(*pic->P18map.INTCON) |= 0x04; // T0IF
(*pic->P18map.TMR0L)++;
if ((!(*pic->P18map.TMR0L)) && (!((*pic->P18map.T0CON) & 0x40))) {
if (((*pic->P18map.TMR0H) + 1) == 0x100)
(*pic->P18map.INTCON) |= 0x04; // T0IF
(*pic->P18map.TMR0H)++;
}
pic->cp0 = 0;
}
}
}
}
pic->t0cki_ = pic->pins[pic->t0cki - 1].value;
}
}
void p18_tmr0_2(_pic *pic) {
if (pic->P18map.T0CON0) {
if ((*pic->P18map.T0CON0) & 0x80) // T0EN
{
if ((((*pic->P18map.T0CON1) & 0xE0) == 0x40) || // TOCS=FOSC/4
((((*pic->P18map.T0CON1) & 0xE0) == 0x00) &&
((pic->t0cki_ == 0) &&
(pic->pins[pic->t0cki - 1].value == 1))) || // T0CS=t0cki
((((*pic->P18map.T0CON1) & 0xE0) == 0x20) &&
((pic->t0cki_ == 1) &&
(pic->pins[pic->t0cki - 1].value == 0)))) // T0CS=t0cki
{
pic->cp0++;
if (pic->lram == sfr_addr(pic->P18map.TMR0L))
pic->cp0 = 0; // RESET prescaler
if (pic->cp0 >= 2 * (fpw2[(*pic->P18map.T0CON1) & 0x0F])) {
(*pic->P18map.TMR0L)++;
// 8 bit mode
if (!((*pic->P18map.T0CON0) & 0x10) &&
((*pic->P18map.TMR0H) == (*pic->P18map.TMR0L))) {
(*pic->P18map.PIR0) |= 0x20; // TMR0IF
(*pic->P18map.TMR0L) = 0;
// TODO buffer TMR0H writes latch
}
// 16 bit mode;
if ((!(*pic->P18map.TMR0L)) && (((*pic->P18map.T0CON0) & 0x10))) {
if (((*pic->P18map.TMR0H) + 1) == 0x100)
(*pic->P18map.PIR0) |= 0x20; // TMR0IF
(*pic->P18map.TMR0H)++;
}
pic->cp0 = 0;
}
// TODO implement postscaler
}
}
pic->t0cki_ = pic->pins[pic->t0cki - 1].value;
}
}