OSDN Git Service

[Qt][OpenGL][WIP] GL4.3: Implementing texture buffer with mapping.
[csp-qt/common_source_project-fm7.git] / source / src / vm / msm5832.cpp
1 /*
2         Skelton for retropc emulator
3
4         Author : Takeda.Toshiya
5         Date   : 2008.05.02-
6
7         [ MSM58321/MSM5832 ]
8 */
9
10 #include "vm.h"
11 #include "../emu.h"
12 #include "msm58321.h"
13
14 #define EVENT_BUSY      0
15 #define EVENT_INC       1
16 #define EVENT_PULSE     2
17
18 #if defined(Q_OS_WIN)
19 DLL_PREFIX_I struct cur_time_s cur_time;
20 #endif
21
22 MSM5832::MSM5832(VM* parent_vm, EMU* parent_emu) : MSM58321_BASE(parent_vm, parent_emu)
23 {
24                 set_device_name(_T("MSM5832 RTC"));
25 }
26
27 void MSM5832::initialize()
28 {
29         DEVICE::initialize();
30         // init rtc
31         memset(regs, 0, sizeof(regs));
32         regs[5] = 8; // 24h
33         regs[15] = 0x0f;
34         wreg = regnum = 0;
35         cs = true;
36         rd = wr = addr_wr = busy = hold = false;
37         count_1024hz = count_1s = count_1m = count_1h = 0;
38         
39         get_host_time(&cur_time);
40         read_from_cur_time();
41         
42         // register events
43 #ifdef MSM58321_START_DAY
44         start_day = MSM58321_START_DAY;
45 #endif
46 #ifdef MSM58321_START_YEAR
47         start_year = MSM58321_START_YEAR;
48 #endif
49         register_event(this, EVENT_INC, 1000000.0, true, &register_id);
50         register_event(this, EVENT_PULSE, 1000000.0 / 8192.0, true, NULL);      // 122.1 usec
51 }
52
53
54 void MSM5832::read_from_cur_time()
55 {
56         // update clock
57         int hour = (regs[5] & 8) ? cur_time.hour : (cur_time.hour % 12);
58         int ampm = (cur_time.hour > 11) ? 4 : 0;
59         
60         regs[ 0] = TO_BCD_LO(cur_time.second);
61         regs[ 1] = TO_BCD_HI(cur_time.second);
62         regs[ 2] = TO_BCD_LO(cur_time.minute);
63         regs[ 3] = TO_BCD_HI(cur_time.minute);
64         regs[ 4] = TO_BCD_LO(hour);
65         regs[ 5] = TO_BCD_HI(hour) | ampm | (regs[5] & 8);
66         regs[ 6] = cur_time.day_of_week;
67         regs[ 7] = TO_BCD_LO(cur_time.day - start_day);
68         regs[ 8] = TO_BCD_HI(cur_time.day - start_day) | (regs[8] & 0x0c);
69         regs[ 9] = TO_BCD_LO(cur_time.month);
70         regs[10] = TO_BCD_HI(cur_time.month);
71         regs[11] = TO_BCD_LO(cur_time.year - start_year);
72         regs[12] = TO_BCD_HI(cur_time.year - start_year);
73 }
74
75 void MSM5832::write_to_cur_time()
76 {
77         cur_time.second = regs[0] + (regs[1] & 7) * 10;
78         cur_time.minute = regs[2] + (regs[3] & 7) * 10;
79         cur_time.hour = regs[4] + (regs[5] & 3) * 10;
80         if(!(regs[5] & 8)) {
81                 cur_time.hour %= 12;
82                 if(regs[5] & 4) {
83                         cur_time.hour += 12;
84                 }
85         }
86 //      cur_time.day_of_week = regs[6] & 7;
87         cur_time.day = regs[7] + (regs[8] & 3) * 10;
88         cur_time.day += start_day;
89         cur_time.month = regs[9] + (regs[10] & 1) * 10;
90         cur_time.year = regs[11] + regs[12] * 10;
91         cur_time.year += start_year;
92         cur_time.update_year();
93         cur_time.update_day_of_week();
94         
95         // restart event
96         cancel_event(this, register_id);
97         register_event(this, EVENT_INC, 1000000.0, true, &register_id);
98 }
99
100 void MSM5832::set_busy(bool val)
101 {
102         busy = val;
103 }