gbdk-2020 | GameBoy Development Kit |
| download: https://git.y1.nz/archives/gbdk.tar.gz | |
| README | Files | Log | Refs | LICENSE |
gbdk-lib/examples/gb/mbc3_rtc/src/main.c
1 #include <gbdk/platform.h>
2
3 #include <gbdk/console.h>
4 #include <stdint.h>
5 #include <stdbool.h>
6 #include <stdio.h>
7
8 #include "mbc3_rtc.h"
9
10
11 // MBC3 RTC example
12 //
13 // Reads and displays the elapsed time since last
14 // reset from the Real Time Clock (RTC) on the MBC3 cart.
15 //
16 // It also allows resetting all the date/time components to zero.
17 //
18 // If your project does not use the included Makefile then
19 // make sure to set the MBC type in the cart header
20 // to 0x10 (RTC + SRAM) using this for lcc: -Wl-yt0x10
21 // Also set the number of cart SRAM banks: -Wm-ya4
22
23
24 // RTC data
25 typedef struct rtc_data_t {
26 uint16_t day;
27 uint8_t hour;
28 uint8_t min;
29 uint8_t sec;
30 } rtc_data_t;
31
32 rtc_data_t rtc;
33
34 static void read_and_show_rtc_data(rtc_data_t * p_rtc);
35 static void write_rtc_data(rtc_data_t * p_rtc);
36
37
38
39 // Example of reading and then displaying RTC date/time
40 static void read_and_show_rtc_data(rtc_data_t * p_rtc) {
41
42 // Read RTC data
43 RTC_LATCH();
44 p_rtc->day = RTC_GET(RTC_VALUE_DAY);
45 p_rtc->hour = RTC_GET(RTC_VALUE_HOUR);
46 p_rtc->min = RTC_GET(RTC_VALUE_MIN);
47 p_rtc->sec = RTC_GET(RTC_VALUE_SEC);
48
49 gotoxy(0,6);
50 printf("Elapsed RTC Time:\n");
51 printf("Days: %u \n"
52 "Time: %u:%u:%u \n",
53 (uint16_t)p_rtc->day,
54 (uint16_t)p_rtc->hour,
55 (uint16_t)p_rtc->min,
56 (uint16_t)p_rtc->sec);
57 }
58
59
60 static void write_rtc_data(rtc_data_t * p_rtc) {
61
62 // In this example just zero out the RTC data
63 // so that when it's read back it will indicate
64 // how much time has elapsed since setting it.
65 p_rtc->day = 0u; // Supported range: 0-511
66 p_rtc->hour = 0u; // Supported range: 0-23
67 p_rtc->min = 0u; // Supported range: 0-59
68 p_rtc->sec = 0u; // Supported range: 0-59
69
70 RTC_SET(RTC_VALUE_DAY, p_rtc->day);
71 RTC_SET(RTC_VALUE_HOUR, p_rtc->hour);
72 RTC_SET(RTC_VALUE_MIN, p_rtc->min);
73 RTC_SET(RTC_VALUE_SEC, p_rtc->sec);
74
75 gotoxy(0,11);
76 printf("Set RTC to:\n");
77 printf("Days: %u \n"
78 "Time: %u:%u:%u \n",
79 (uint16_t)p_rtc->day,
80 (uint16_t)p_rtc->hour,
81 (uint16_t)p_rtc->min,
82 (uint16_t)p_rtc->sec);
83 }
84
85
86 static void clear_rtc_send_message(void) {
87
88 gotoxy(0,11);
89 printf(" \n");
90 printf(" \n"
91 " \n");
92 }
93
94
95 void main(void) {
96
97 // Enable RAM for RTC register access and start the RTC ticking
98 ENABLE_RAM;
99 RTC_START(true);
100
101 uint8_t gamepad;
102
103 SPRITES_8x8;
104 SHOW_SPRITES;
105 SHOW_BKG;
106
107 printf("RTC Date/Time Demo\n"
108 "\n"
109 "* Press SELECT to \n"
110 " set new RTC info.\n");
111
112 uint8_t clear_notice_count = 0;
113 while(1) {
114
115 // Wait for the start of a new frame, then read the gamepad
116 vsync();
117 gamepad = joypad();
118
119 // Send RTC data to device if SELECT is pressed
120 // otherwise check if it's time to read and show RTC data
121 if (gamepad & J_SELECT) {
122 write_rtc_data(&rtc);
123 // Queue clearing the "sent rtc date/time" message after a few seconds
124 clear_notice_count = 6;
125
126 // Wait until SELECT is released before continuing
127 waitpadup();
128 }
129 else {
130 // Update Displayed RTC data every 32 frames (about twice per second)
131 if ((sys_time & 0x1Fu) == 0u) {
132 read_and_show_rtc_data(&rtc);
133
134 if (clear_notice_count) {
135 clear_notice_count--;
136 if (clear_notice_count == 0) {
137 clear_rtc_send_message();
138 }
139 }
140 }
141 }
142
143 }
144
145 }
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.