forked from AppleWin/AppleWin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNoSlotClock.cpp
More file actions
224 lines (189 loc) · 5.46 KB
/
Copy pathNoSlotClock.cpp
File metadata and controls
224 lines (189 loc) · 5.46 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
AppleWin : An Apple //e emulator for Windows
Copyright (C) 1994-1996, Michael O'Brien
Copyright (C) 1999-2001, Oliver Schmidt
Copyright (C) 2002-2005, Tom Charlesworth
Copyright (C) 2006-2010, Tom Charlesworth, Michael Pohoreski, Nick Westgate
AppleWin 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 of the License, or
(at your option) any later version.
AppleWin 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 AppleWin; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* Description: No Slot Clock/Phantom Clock (Dallas SmartWatch DS1216) emulation
*
* Author: Nick Westgate
*/
/* Posted to csa2, "No Slot Clock and Day Of Week apps?" by Nick on 21/06/2011:
DOW interpretation is only a convention, but unfortunately it seems Dallas chose a different
convention from the original NSC vendors. And perhaps the NSC vendors then adopted the new convention.
This conclusion is drawn from the 3 available data points:
- Original (1986/1987) NSC manual: 1=MON
- SmartWatch Utility (1987) v1.1: 1=SUN
- No Slot Clock Utilities (1991) v.14: 1=SUN
All the other drivers and utilities available to me don't define the DOW mapping.
*/
#include "StdAfx.h"
#include "NoSlotClock.h"
CNoSlotClock::CNoSlotClock()
:
m_ClockRegister(),
m_ComparisonRegister(kClockInitSequence)
{
Reset();
}
void CNoSlotClock::Reset()
{
// SmartWatch reset - whether tied to system reset is component specific
m_ComparisonRegister.Reset();
m_bClockRegisterEnabled = false;
m_bWriteEnabled = true;
}
bool CNoSlotClock::Read(int address, int& data)
{
// this may read or write the clock (returns true if data is changed)
if (address & 0x04)
return ClockRead(data);
else
{
ClockWrite(address);
return false;
}
}
void CNoSlotClock::Write(int address)
{
// this may read or write the clock
int dummy = 0;
if (address & 0x04)
ClockRead(dummy);
else
ClockWrite(address);
}
bool CNoSlotClock::ClockRead(int& data)
{
// for a ROM, A2 high = read, and data out (if any) is on D0
if (!m_bClockRegisterEnabled)
{
m_ComparisonRegister.Reset();
m_bWriteEnabled = true;
return false;
}
else
{
m_ClockRegister.ReadBit(data);
if (m_ClockRegister.NextBit())
m_bClockRegisterEnabled = false;
return true;
}
}
void CNoSlotClock::ClockWrite(int address)
{
// for a ROM, A2 low = write, and data in is on A0
if (!m_bWriteEnabled)
return;
if (!m_bClockRegisterEnabled)
{
if ((m_ComparisonRegister.CompareBit(address & 0x1)))
{
if (m_ComparisonRegister.NextBit())
{
m_bClockRegisterEnabled = true;
PopulateClockRegister();
}
}
else
{
// mismatch ignores further writes
m_bWriteEnabled = false;
}
}
else if (m_ClockRegister.NextBit())
{
// simulate writes, but our clock register is read-only
m_bClockRegisterEnabled = false;
}
}
void CNoSlotClock::PopulateClockRegister()
{
// all values are in packed BCD format (4 bits per decimal digit)
SYSTEMTIME now;
GetLocalTime(&now);
int centisecond = now.wMilliseconds / 10; // 00-99
m_ClockRegister.WriteNibble(centisecond % 10);
m_ClockRegister.WriteNibble(centisecond / 10);
int second = now.wSecond; // 00-59
m_ClockRegister.WriteNibble(second % 10);
m_ClockRegister.WriteNibble(second / 10);
int minute = now.wMinute; // 00-59
m_ClockRegister.WriteNibble(minute % 10);
m_ClockRegister.WriteNibble(minute / 10);
int hour = now.wHour; // 01-23
m_ClockRegister.WriteNibble(hour % 10);
m_ClockRegister.WriteNibble(hour / 10);
int day = now.wDayOfWeek + 1; // 01-07 (1 = Sunday)
m_ClockRegister.WriteNibble(day % 10);
m_ClockRegister.WriteNibble(day / 10);
int date = now.wDay; // 01-31
m_ClockRegister.WriteNibble(date % 10);
m_ClockRegister.WriteNibble(date / 10);
int month = now.wMonth; // 01-12
m_ClockRegister.WriteNibble(month % 10);
m_ClockRegister.WriteNibble(month / 10);
int year = now.wYear % 100; // 00-99
m_ClockRegister.WriteNibble(year % 10);
m_ClockRegister.WriteNibble(year / 10);
}
CNoSlotClock::RingRegister64::RingRegister64()
{
Reset();
m_Register = 0;
}
CNoSlotClock::RingRegister64::RingRegister64(UINT64 data)
{
Reset();
m_Register = data;
}
void CNoSlotClock::RingRegister64::Reset()
{
m_Mask = 1;
}
void CNoSlotClock::RingRegister64::WriteNibble(int data)
{
WriteBits(data, 4);
}
void CNoSlotClock::RingRegister64::WriteBits(int data, int count)
{
for (int i = 1; i <= count; i++)
{
WriteBit(data);
NextBit();
data >>= 1;
}
}
void CNoSlotClock::RingRegister64::WriteBit(int data)
{
m_Register = (data & 0x1) ? (m_Register | m_Mask) : (m_Register & ~m_Mask);
}
void CNoSlotClock::RingRegister64::ReadBit(int& data)
{
data = (m_Register & m_Mask) ? data | 1 : data & ~1;
}
bool CNoSlotClock::RingRegister64::CompareBit(int data)
{
return ((m_Register & m_Mask) != 0) == ((data & 1) != 0);
}
bool CNoSlotClock::RingRegister64::NextBit()
{
if ((m_Mask <<= 1) == 0)
{
m_Mask = 1;
return true; // wrap
}
return false;
}