| 1 |
/* |
| 2 |
Copyright (C) 2010 Roy R Rankin |
| 3 |
Copyright (C) 2006 T. Scott Dattalo |
| 4 |
|
| 5 |
This file is part of gpsim. |
| 6 |
|
| 7 |
gpsim is free software; you can redistribute it and/or modify |
| 8 |
it under the terms of the GNU General Public License as published by |
| 9 |
the Free Software Foundation; either version 2, or (at your option) |
| 10 |
any later version. |
| 11 |
|
| 12 |
gpsim is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
GNU General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along with gpsim; see the file COPYING. If not, write to |
| 19 |
the Free Software Foundation, 59 Temple Place - Suite 330, |
| 20 |
Boston, MA 02111-1307, USA. */ |
| 21 |
|
| 22 |
|
| 23 |
#include <stdio.h> |
| 24 |
#include <time.h> |
| 25 |
|
| 26 |
//#define DEBUG |
| 27 |
#if defined(DEBUG) |
| 28 |
#define Dprintf(arg) {printf("%s:%d ",__FILE__,__LINE__); printf arg; } |
| 29 |
#else |
| 30 |
#define Dprintf(arg) {} |
| 31 |
#endif |
| 32 |
|
| 33 |
#include "config.h" // get the definition for HAVE_GUI |
| 34 |
#ifdef HAVE_GUI |
| 35 |
#include <gtk/gtk.h> |
| 36 |
#endif |
| 37 |
|
| 38 |
#include "src/i2c-ee.h" |
| 39 |
#include "ds1307.h" |
| 40 |
#include "src/gpsim_time.h" |
| 41 |
#include "src/stimuli.h" |
| 42 |
#include "src/ioports.h" |
| 43 |
#include "src/symbol.h" |
| 44 |
#include "src/value.h" |
| 45 |
#include "src/packages.h" |
| 46 |
#include "src/gpsim_interface.h" |
| 47 |
|
| 48 |
class Processor; |
| 49 |
|
| 50 |
I2C_RTC::I2C_RTC(Processor *pCpu, unsigned int _rom_size, |
| 51 |
unsigned int _write_page_size, |
| 52 |
unsigned int _addr_bytes, unsigned int _CSmask, |
| 53 |
unsigned int _BSmask, unsigned int _BSshift) |
| 54 |
: I2C_EE(pCpu, _rom_size, _write_page_size, |
| 55 |
_addr_bytes, _CSmask, _BSmask, _BSshift) |
| 56 |
{ |
| 57 |
pEE = (DS1307_Modules::ds1307 *)pCpu; |
| 58 |
i2c_slave_address = 0xd0; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
bool I2C_RTC::match_address() |
| 63 |
{ |
| 64 |
Dprintf(("match_address I2C got address=0x%x (w=%d) want 0x%x\n", (xfr_data & 0xfe), (xfr_data & 0x01), i2c_slave_address)); |
| 65 |
return ((xfr_data & 0xfe) == i2c_slave_address); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
class SQW_PIN : public IO_open_collector { |
| 70 |
public: |
| 71 |
explicit SQW_PIN(const char *_name) |
| 72 |
: IO_open_collector(_name) |
| 73 |
{ |
| 74 |
bDrivingState = true; |
| 75 |
bDrivenState = true; |
| 76 |
// Make the pin an output. |
| 77 |
update_direction(IO_bi_directional::DIR_OUTPUT, true); |
| 78 |
} |
| 79 |
|
| 80 |
void setDrivingState(bool new_state) |
| 81 |
{ |
| 82 |
bDrivingState = new_state; |
| 83 |
bDrivenState = new_state; |
| 84 |
|
| 85 |
if (snode) { |
| 86 |
snode->update(); |
| 87 |
} |
| 88 |
} |
| 89 |
}; |
| 90 |
|
| 91 |
|
| 92 |
namespace DS1307_Modules { |
| 93 |
|
| 94 |
ds1307::ds1307(const char *_name) |
| 95 |
: Module(_name, "BS1307"), |
| 96 |
m_eeprom(nullptr), m_sqw(nullptr), |
| 97 |
chip_select(0), |
| 98 |
att_eeprom(nullptr), |
| 99 |
next_clock_tick(0), next_sqw_edge(0), sqw_interval(0), |
| 100 |
out(false) |
| 101 |
{ |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
ds1307::~ds1307() |
| 106 |
{ |
| 107 |
removeSymbol(m_sqw); |
| 108 |
removeSymbol((IOPIN *)(m_eeprom->sda)); |
| 109 |
removeSymbol((IOPIN *)(m_eeprom->scl)); |
| 110 |
m_eeprom->sda = 0; |
| 111 |
m_eeprom->scl = 0; |
| 112 |
delete att_eeprom; |
| 113 |
delete m_eeprom; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
Module *ds1307::construct_ds1307(const char *_new_name) |
| 118 |
{ |
| 119 |
// std::string att_name = _new_name; ??? Does not appear to have a use |
| 120 |
ds1307 *pEE = new ds1307(_new_name); |
| 121 |
// I2C_EE size in bytes prom size in bits |
| 122 |
(pEE->m_eeprom) = new I2C_RTC((Processor *)pEE, 64, 16, 1, 0xe, 0, 0); |
| 123 |
pEE->create_iopin_map(); |
| 124 |
// att_name += ".ds1307"; |
| 125 |
pEE->att_eeprom = new PromAddress(pEE->m_eeprom, "eeprom", "Address I2C_RTC"); |
| 126 |
pEE->addSymbol(pEE->att_eeprom); |
| 127 |
#ifdef LOCAL_TIME |
| 128 |
struct tm *tm; |
| 129 |
time_t t = time(nullptr); |
| 130 |
int val; |
| 131 |
tm = localtime(&t); |
| 132 |
val = tm->tm_sec; |
| 133 |
(pEE->m_eeprom->get_register(0))->put((val % 10) + ((val / 10) << 4)); |
| 134 |
val = tm->tm_min; |
| 135 |
(pEE->m_eeprom->get_register(1))->put((val % 10) + ((val / 10) << 4)); |
| 136 |
val = tm->tm_hour; |
| 137 |
(pEE->m_eeprom->get_register(2))->put((val % 10) + ((val / 10) << 4) + 0x40); |
| 138 |
(pEE->m_eeprom->get_register(3))->put(tm->tm_wday + 1); |
| 139 |
val = tm->tm_mday; |
| 140 |
(pEE->m_eeprom->get_register(4))->put((val % 10) + ((val / 10) << 4)); |
| 141 |
val = tm->tm_mon + 1; |
| 142 |
(pEE->m_eeprom->get_register(5))->put((val % 10) + ((val / 10) << 4)); |
| 143 |
val = tm->tm_year % 100; |
| 144 |
(pEE->m_eeprom->get_register(6))->put((val % 10) + ((val / 10) << 4)); |
| 145 |
(pEE->m_eeprom->get_register(7))->put(0x10); |
| 146 |
pEE->controlWritten(0x10); |
| 147 |
#else |
| 148 |
// factory defaults |
| 149 |
(pEE->m_eeprom->get_register(0))->put(0x80); |
| 150 |
(pEE->m_eeprom->get_register(1))->put(0x00); |
| 151 |
(pEE->m_eeprom->get_register(2))->put(0x00); |
| 152 |
(pEE->m_eeprom->get_register(3))->put(0x01); |
| 153 |
(pEE->m_eeprom->get_register(4))->put(0x01); |
| 154 |
(pEE->m_eeprom->get_register(5))->put(0x01); |
| 155 |
(pEE->m_eeprom->get_register(6))->put(0x00); |
| 156 |
(pEE->m_eeprom->get_register(7))->put(0x00); |
| 157 |
#endif |
| 158 |
return pEE; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
void ds1307::create_iopin_map() |
| 163 |
{ |
| 164 |
m_sqw = new SQW_PIN("SQW"); |
| 165 |
addSymbol(m_sqw); |
| 166 |
addSymbol((IOPIN *)(m_eeprom->sda)); |
| 167 |
addSymbol((IOPIN *)(m_eeprom->scl)); |
| 168 |
package = new Package(8); |
| 169 |
package->assign_pin(1, 0); |
| 170 |
package->assign_pin(2, 0); |
| 171 |
package->assign_pin(3, 0); |
| 172 |
package->assign_pin(5, (IOPIN *)(m_eeprom->sda)); |
| 173 |
package->assign_pin(6, (IOPIN *)(m_eeprom->scl)); |
| 174 |
package->assign_pin(7, m_sqw); |
| 175 |
m_sqw->update_direction(1, true); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
void ds1307::controlWritten(unsigned int cntl) |
| 180 |
{ |
| 181 |
if (cntl & SQWE) { |
| 182 |
unsigned int new_sqw_interval = 0; |
| 183 |
|
| 184 |
switch (cntl & (RS1 | RS0)) { |
| 185 |
case 0: |
| 186 |
new_sqw_interval = 0.5 / (get_cycles().seconds_per_cycle()); |
| 187 |
break; |
| 188 |
|
| 189 |
case 1: |
| 190 |
new_sqw_interval = 0.5 / (4096.0 * get_cycles().seconds_per_cycle()); |
| 191 |
break; |
| 192 |
|
| 193 |
case 2: |
| 194 |
new_sqw_interval = 0.5 / (8192.0 * get_cycles().seconds_per_cycle()); |
| 195 |
break; |
| 196 |
|
| 197 |
case 3: |
| 198 |
new_sqw_interval = 0.5 / (32768.0 * get_cycles().seconds_per_cycle()); |
| 199 |
break; |
| 200 |
} |
| 201 |
|
| 202 |
if (!new_sqw_interval) { |
| 203 |
fprintf(stderr, "DS1307 SQW faster than can be simulated\n"); |
| 204 |
new_sqw_interval = 1; |
| 205 |
} |
| 206 |
|
| 207 |
int sec = (m_eeprom->get_register(0))->get(); |
| 208 |
|
| 209 |
if (!(sec & CH)) { |
| 210 |
if (next_sqw_edge) { |
| 211 |
if (new_sqw_interval != sqw_interval) { |
| 212 |
get_cycles().clear_break(next_sqw_edge); |
| 213 |
next_sqw_edge = new_sqw_interval - sqw_interval; |
| 214 |
get_cycles().set_break(next_sqw_edge, this); |
| 215 |
} |
| 216 |
|
| 217 |
} else { |
| 218 |
out = false; |
| 219 |
m_sqw->setDrivingState(out); |
| 220 |
next_sqw_edge = get_cycles().get() + new_sqw_interval; |
| 221 |
get_cycles().set_break(next_sqw_edge, this); |
| 222 |
} |
| 223 |
|
| 224 |
if (!next_clock_tick) { |
| 225 |
next_clock_tick = get_cycles().get() + get_cycles().instruction_cps(); |
| 226 |
get_cycles().set_break(next_clock_tick, this); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
sqw_interval = new_sqw_interval; |
| 231 |
|
| 232 |
} else { |
| 233 |
sqw_interval = 0; |
| 234 |
|
| 235 |
if (next_sqw_edge) { // stop the SQW output |
| 236 |
get_cycles().clear_break(next_sqw_edge); |
| 237 |
next_sqw_edge = 0; |
| 238 |
} |
| 239 |
|
| 240 |
m_sqw->setDrivingState((cntl & OUT) == OUT); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
void ds1307::secWritten(unsigned int sec) |
| 246 |
{ |
| 247 |
if (sec & CH) { // Turn off clock functions |
| 248 |
Dprintf(("DS1307 Turn off clock\n")); |
| 249 |
|
| 250 |
if (next_sqw_edge) { // stop the SQW output |
| 251 |
get_cycles().clear_break(next_sqw_edge); |
| 252 |
next_sqw_edge = 0; |
| 253 |
} |
| 254 |
|
| 255 |
if (next_clock_tick) { // stop the SQW output |
| 256 |
get_cycles().clear_break(next_clock_tick); |
| 257 |
next_clock_tick = 0; |
| 258 |
} |
| 259 |
|
| 260 |
} else { // Clock functions on |
| 261 |
Dprintf(("Clock functions on\n")); |
| 262 |
|
| 263 |
if (next_clock_tick) { // resync ticks |
| 264 |
get_cycles().clear_break(next_clock_tick); |
| 265 |
} |
| 266 |
|
| 267 |
next_clock_tick = get_cycles().get() + get_cycles().instruction_cps(); |
| 268 |
get_cycles().set_break(next_clock_tick, this); |
| 269 |
|
| 270 |
if (next_sqw_edge) { |
| 271 |
get_cycles().clear_break(next_sqw_edge); |
| 272 |
} |
| 273 |
|
| 274 |
if (sqw_interval) { |
| 275 |
next_sqw_edge = get_cycles().get() + sqw_interval; |
| 276 |
get_cycles().set_break(next_sqw_edge, this); |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
void ds1307::incrementRTC() |
| 283 |
{ |
| 284 |
int i; |
| 285 |
int value; |
| 286 |
bool is12h; |
| 287 |
unsigned char dom; |
| 288 |
unsigned char month; |
| 289 |
unsigned int year; |
| 290 |
unsigned char dim[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; |
| 291 |
value = (m_eeprom->get_register(0))->get(); |
| 292 |
|
| 293 |
if (value & 0x80) { // return if clock halt set |
| 294 |
return; |
| 295 |
} |
| 296 |
|
| 297 |
i = (value >> 4) * 10 + (value & 0xf) + 1; |
| 298 |
|
| 299 |
if (i < 60) { |
| 300 |
value = i % 10; |
| 301 |
value = (i >= 10) ? value | ((i / 10) << 4) : value; |
| 302 |
(m_eeprom->get_register(0))->put(value); |
| 303 |
return; |
| 304 |
} |
| 305 |
|
| 306 |
(m_eeprom->get_register(0))->put(0); |
| 307 |
value = (m_eeprom->get_register(1))->get(); // Get minutes |
| 308 |
i = (value >> 4) * 10 + (value & 0xf) + 1; |
| 309 |
|
| 310 |
if (i < 60) { |
| 311 |
value = i % 10; |
| 312 |
value = (i >= 10) ? value | ((i / 10) << 4) : value; |
| 313 |
(m_eeprom->get_register(1))->put(value); |
| 314 |
return; |
| 315 |
} |
| 316 |
|
| 317 |
(m_eeprom->get_register(1))->put(0); |
| 318 |
value = (m_eeprom->get_register(2))->get(); // Get Hours |
| 319 |
is12h = (value & 0x40) == 0x40; |
| 320 |
|
| 321 |
if (is12h) { |
| 322 |
bool isPM; |
| 323 |
isPM = (value & 0x20) == 0x20; |
| 324 |
i = ((value & 1) >> 4) * 10 + (value & 0xf) + 1; |
| 325 |
|
| 326 |
if (i == 12 && ! isPM) { |
| 327 |
isPM = true; |
| 328 |
i = 0; |
| 329 |
} |
| 330 |
|
| 331 |
if (i < 12) { |
| 332 |
value = i % 10; |
| 333 |
value = (i >= 10) ? value | ((i / 10) << 4) : value; |
| 334 |
value |= isPM ? 0x60 : 0x40; |
| 335 |
(m_eeprom->get_register(2))->put(value); |
| 336 |
return; |
| 337 |
} |
| 338 |
|
| 339 |
value = 0x40; |
| 340 |
(m_eeprom->get_register(2))->put(value); |
| 341 |
|
| 342 |
} else { |
| 343 |
i = (value >> 4) * 10 + (value & 0xf) + 1; |
| 344 |
|
| 345 |
if (i < 24) { |
| 346 |
value = i % 10; |
| 347 |
value = (i >= 10) ? value | ((i / 10) << 4) : value; |
| 348 |
(m_eeprom->get_register(2))->put(value); |
| 349 |
return; |
| 350 |
} |
| 351 |
|
| 352 |
(m_eeprom->get_register(2))->put(0); |
| 353 |
} |
| 354 |
|
| 355 |
value = (m_eeprom->get_register(3))->get(); // Get DOW |
| 356 |
value = (value + 1) % 7; |
| 357 |
(m_eeprom->get_register(3))->put(value); |
| 358 |
value = (m_eeprom->get_register(4))->get(); // Get DOM |
| 359 |
dom = (value >> 4) * 10 + (value & 0xf) + 1; |
| 360 |
value = (m_eeprom->get_register(5))->get(); // Get month |
| 361 |
month = (value >> 4) * 10 + (value & 0xf); |
| 362 |
value = (m_eeprom->get_register(6))->get(); // Get year |
| 363 |
year = (value >> 4) * 10 + (value & 0xf) + 2000; |
| 364 |
|
| 365 |
if ((year % 400) == 0 || ((year % 4) == 0 && (year % 100) != 0)) { |
| 366 |
dim[1] = 29; |
| 367 |
} |
| 368 |
|
| 369 |
if (dom <= dim[month - 1]) { |
| 370 |
value = dom % 10; |
| 371 |
value = (dom >= 10) ? value | ((dom / 10) << 4) : value; |
| 372 |
(m_eeprom->get_register(4))->put(value); |
| 373 |
return; |
| 374 |
} |
| 375 |
|
| 376 |
(m_eeprom->get_register(4))->put(1); |
| 377 |
|
| 378 |
if (++month <= 12) { |
| 379 |
value = month % 10; |
| 380 |
value = (month >= 10) ? value | ((month / 10) << 4) : value; |
| 381 |
(m_eeprom->get_register(5))->put(value); |
| 382 |
return; |
| 383 |
} |
| 384 |
|
| 385 |
(m_eeprom->get_register(5))->put(1); |
| 386 |
i = ++year % 100; |
| 387 |
value = i % 10; |
| 388 |
value = (i >= 10) ? value | ((i / 10) << 4) : value; |
| 389 |
(m_eeprom->get_register(6))->put(value); |
| 390 |
return; |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
void ds1307::callback() |
| 395 |
{ |
| 396 |
guint64 now = get_cycles().get(); |
| 397 |
Dprintf(("ds1307 now=%ld tick= %ld sqw= %ld\n", |
| 398 |
(long)now, (long)next_clock_tick, (long)next_sqw_edge)); |
| 399 |
|
| 400 |
if (now == next_clock_tick) { |
| 401 |
incrementRTC(); |
| 402 |
next_clock_tick = get_cycles().get() + get_cycles().instruction_cps(); |
| 403 |
get_cycles().set_break(next_clock_tick, this); |
| 404 |
} |
| 405 |
|
| 406 |
if (now == next_sqw_edge) { |
| 407 |
long diff; |
| 408 |
out = !out; |
| 409 |
next_sqw_edge = get_cycles().get() + sqw_interval; |
| 410 |
// syncronize with clock ticks |
| 411 |
diff = labs((long)(next_sqw_edge - next_clock_tick)); |
| 412 |
|
| 413 |
if (diff < (long)sqw_interval / 2) { |
| 414 |
if (!out) { |
| 415 |
fprintf(stderr, "DS1307 SQW phase issue\n"); |
| 416 |
} |
| 417 |
|
| 418 |
next_sqw_edge = next_clock_tick; |
| 419 |
} |
| 420 |
|
| 421 |
m_sqw->setDrivingState(out); |
| 422 |
get_cycles().set_break(next_sqw_edge, this); |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
} // end of namespace DS1307_Modules |