SBird1337 6 роки тому
джерело
коміт
3c9403bab4
4 змінених файлів з 136 додано та 1 видалено
  1. 4
    0
      bpre.sym
  2. 18
    0
      src/include/rtc.h
  3. 4
    1
      src/overworld/dns/dns.c
  4. 110
    0
      src/rtc/rtc.c

+ 4
- 0
bpre.sym Переглянути файл

@@ -203,6 +203,10 @@ current_map_block_role_get = 0x08058F78|1;
203 203
 
204 204
 current_oe_state = 0x020386E0;
205 205
 
206
+rtc_data = 0x080000C4;
207
+rtc_direction = 0x080000C6;
208
+rtc_control = 0x080000C8;
209
+
206 210
 /*__aeabi_idiv = 0x081E4018|1;
207 211
 __aeabi_idivmod = 0x081E40F4|1;
208 212
 __aeabi_uidiv = 0x081E460C|1;

+ 18
- 0
src/include/rtc.h Переглянути файл

@@ -0,0 +1,18 @@
1
+#ifndef RTC_H_
2
+#define RTC_H_
3
+
4
+#include <pokeagb/pokeagb.h>
5
+
6
+struct RtcTimestamp {
7
+    u8 year;
8
+    u8 month;
9
+    u8 day;
10
+    u8 day_of_week;
11
+    u8 hour;
12
+    u8 minute;
13
+    u8 second;
14
+};
15
+
16
+void rtc_get_time(struct RtcTimestamp* out);
17
+
18
+#endif

+ 4
- 1
src/overworld/dns/dns.c Переглянути файл

@@ -1,6 +1,7 @@
1 1
 #include <pokeagb/pokeagb.h>
2 2
 #include <agb_debug.h>
3 3
 #include <dns.h>
4
+#include <rtc.h>
4 5
 
5 6
 #define DNS_BUF_SIZE 256
6 7
 #define DNS_SEC_PAL_START 224
@@ -62,6 +63,9 @@ void dns_blockset_load_palette(struct MapBlockset* blockset, u16 offset, u16 siz
62 63
 }
63 64
 
64 65
 void dns_oec01_load_pal_impl(u32 *oe_script) {
66
+    struct RtcTimestamp stamp;
67
+    rtc_get_time(&stamp);
68
+    dprintf("rtc data was read: %x.%x.%x\n", stamp.day, stamp.month, stamp.year);
65 69
     struct SpritePalette *pal = (struct SpritePalette *)oe_read_word(oe_script);
66 70
     struct SpritePalette palToApply = {
67 71
         .data = NULL,
@@ -115,7 +119,6 @@ void dns_pal_patch_for_npc(u16 tag, u8 idx)
115 119
     memcpy(buffer, ow_pal_table[npcPalIdx].data,32);
116 120
     dns_modify_palette(buffer,16);
117 121
     gpu_pal_apply(buffer, 256 + 16*idx, 32);
118
-
119 122
     free(buffer);
120 123
     tint_palette_switch(idx);
121 124
 }

+ 110
- 0
src/rtc/rtc.c Переглянути файл

@@ -0,0 +1,110 @@
1
+#include <pokeagb/pokeagb.h>
2
+
3
+#define HIGH 1
4
+#define LOW 0
5
+#define IN 0
6
+#define OUT 1
7
+
8
+struct RtcTimestamp {
9
+    u8 year;
10
+    u8 month;
11
+    u8 day;
12
+    u8 day_of_week;
13
+    u8 hour;
14
+    u8 minute;
15
+    u8 second;
16
+};
17
+
18
+struct RtcGpioData {
19
+    u16 sck : 1;
20
+    u16 sio : 1;
21
+    u16 cs : 1;
22
+    u16 unused : 1;
23
+    u16 free : 12;
24
+};
25
+
26
+ASSERT_SIZEOF(struct RtcGpioData, 2);
27
+
28
+struct RtcGpioDirection {
29
+    u16 sck : 1;
30
+    u16 sio : 1;
31
+    u16 cs : 1;
32
+    u16 unused : 1;
33
+    u16 free : 12;
34
+};
35
+
36
+ASSERT_SIZEOF(struct RtcGpioDirection, 2);
37
+
38
+extern struct RtcGpioData rtc_data;
39
+extern struct RtcGpioDirection rtc_direction;
40
+extern u16 rtc_control;
41
+
42
+void rtc_wait_cycles(volatile u16 cycles);
43
+void rtc_write(u8 b);
44
+u8 rtc_read(void);
45
+
46
+void rtc_get_time(struct RtcTimestamp* out)
47
+{
48
+    rtc_control = 1;
49
+
50
+    rtc_direction.sck = OUT;
51
+    rtc_direction.cs = OUT;
52
+    rtc_data.cs = LOW;
53
+    rtc_data.sck = HIGH;
54
+
55
+    rtc_wait_cycles(20);
56
+
57
+    rtc_data.cs = HIGH;
58
+    rtc_wait_cycles(20);
59
+    rtc_direction.sck = OUT;
60
+    rtc_direction.cs = OUT;
61
+    rtc_write(0x65);
62
+
63
+    out->year = rtc_read();
64
+    out->month = rtc_read();
65
+    out->day = rtc_read();
66
+    out->day_of_week = rtc_read();
67
+    out->hour = rtc_read();
68
+    out->minute = rtc_read();
69
+    out->second = rtc_read();
70
+
71
+    rtc_data.sck = LOW;
72
+    rtc_data.sio = LOW;
73
+    rtc_data.cs = LOW;
74
+}
75
+
76
+void rtc_write(u8 b){
77
+    rtc_direction.sio = OUT;
78
+    rtc_direction.sck = OUT;
79
+    for(u8 i = 0; i < 8; ++i) {
80
+        rtc_data.sck = LOW;
81
+        rtc_data.sio = (b & 0x80) >> 7;
82
+        b <<= 1;
83
+        rtc_wait_cycles(100);
84
+        rtc_data.sck = HIGH;
85
+        rtc_wait_cycles(100);
86
+    }
87
+}
88
+
89
+u8 rtc_read(void){
90
+    u8 val = 0;
91
+    rtc_direction.sio = IN;
92
+    rtc_direction.sck = OUT;
93
+    for(u8 i = 0; i < 8; ++i) {
94
+        rtc_data.sck = LOW;
95
+        rtc_wait_cycles(100);
96
+        rtc_data.sck = HIGH;
97
+        val |= (rtc_data.sio << i);
98
+        rtc_wait_cycles(100);
99
+    }
100
+    return val;
101
+}
102
+
103
+//this is a blocking wait, because fk IRQ
104
+void rtc_wait_cycles(volatile u16 cycles)
105
+{
106
+    while(cycles > 0)
107
+    {
108
+        cycles--;
109
+    }
110
+}