Browse Source

start work on multiplayer mode

Thomas B 1 month ago
parent
commit
bf1995ff71
6 changed files with 216 additions and 3 deletions
  1. 13
    0
      src/main.c
  2. 0
    3
      src/main.h
  3. 144
    0
      src/multiplayer.c
  4. 33
    0
      src/multiplayer.h
  5. 24
    0
      src/window.c
  6. 2
    0
      src/window.h

+ 13
- 0
src/main.c View File

@@ -39,6 +39,7 @@
39 39
 #include "sample.h"
40 40
 #include "window.h"
41 41
 #include "gbprinter.h"
42
+#include "multiplayer.h"
42 43
 #include "main.h"
43 44
 
44 45
 uint8_t debug_menu_index = 0;
@@ -116,6 +117,12 @@ static void about_screen(void) NONBANKED {
116 117
     while (1) {
117 118
         key_read();
118 119
 
120
+        if (mp_master_ready()) {
121
+            mp_master_start();
122
+            break;
123
+        }
124
+        win_about_mp();
125
+
119 126
         if (key_pressed(J_A) || key_pressed(J_B) || key_pressed(J_SELECT)) {
120 127
             break;
121 128
         }
@@ -313,6 +320,12 @@ static void splash(void) NONBANKED {
313 320
     while (1) {
314 321
         key_read();
315 322
 
323
+        if (mp_slave_ready()) {
324
+            mp_slave_start();
325
+            splash_win();
326
+        }
327
+        win_splash_mp();
328
+
316 329
         if (key_pressed(J_LEFT) && (!(conf_get()->debug_flags & DBG_MENU))) {
317 330
             highscore(1);
318 331
             splash_win();

+ 0
- 3
src/main.h View File

@@ -4,9 +4,6 @@
4 4
  *
5 5
  * Copyright (C) 2025 Thomas Buck <thomas@xythobuz.de>
6 6
  *
7
- * Based on examples from gbdk-2020:
8
- * https://github.com/gbdk-2020/gbdk-2020/blob/develop/gbdk-lib/examples/gb/rand/rand.c
9
- *
10 7
  * This program is free software: you can redistribute it and/or modify
11 8
  * it under the terms of the GNU General Public License as published by
12 9
  * the Free Software Foundation, either version 3 of the License, or

+ 144
- 0
src/multiplayer.c View File

@@ -0,0 +1,144 @@
1
+/*
2
+ * multiplayer.c
3
+ * Duality
4
+ *
5
+ * Copyright (C) 2025 Thomas Buck <thomas@xythobuz.de>
6
+ *
7
+ * This program 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 3 of the License, or
10
+ * (at your option) any later version.
11
+ *
12
+ * This program 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
+ * See <http://www.gnu.org/licenses/>.
18
+ */
19
+
20
+#include "game.h"
21
+#include "timer.h"
22
+#include "multiplayer.h"
23
+
24
+#define MASTER_HELLO 0x42
25
+#define SLAVE_HELLO 0x23
26
+
27
+#define RETRANSMIT_TIME 200
28
+
29
+enum mp_state {
30
+    MP_M_SEND = 0,
31
+    MP_M_WAIT = 1,
32
+    MP_M_REPLY = 2,
33
+
34
+    MP_S_START = 0,
35
+    MP_S_WAIT = 1,
36
+    MP_S_REPLY = 2,
37
+};
38
+
39
+static enum mp_state state = 0;
40
+static uint16_t next_t = 0;
41
+
42
+uint8_t mp_connection_status = 0;
43
+
44
+uint8_t mp_master_ready(void) BANKED {
45
+    switch (state) {
46
+        case MP_M_SEND:
47
+            SB_REG = MASTER_HELLO;
48
+            SC_REG = SIOF_XFER_START | SIOF_CLOCK_INT;
49
+            next_t = timer_get() + RETRANSMIT_TIME;
50
+            mp_connection_status++;
51
+            state = MP_M_WAIT;
52
+            break;
53
+
54
+        case MP_M_WAIT:
55
+            if (!(SC_REG & SIOF_XFER_START)) {
56
+                if (SB_REG == SLAVE_HELLO) {
57
+                    SB_REG = SLAVE_HELLO;
58
+                    SC_REG = SIOF_XFER_START | SIOF_CLOCK_EXT;
59
+                    next_t = timer_get() + RETRANSMIT_TIME;
60
+                    mp_connection_status++;
61
+                    state = MP_M_REPLY;
62
+                }
63
+            }
64
+            if (timer_get() >= next_t) {
65
+                state = MP_M_SEND;
66
+            }
67
+            break;
68
+
69
+        case MP_M_REPLY:
70
+            if (!(SC_REG & SIOF_XFER_START)) {
71
+                if (SB_REG == MASTER_HELLO) {
72
+                    return 1;
73
+                } else {
74
+                    state = MP_M_SEND;
75
+                }
76
+            }
77
+            if (timer_get() >= next_t) {
78
+                state = MP_M_SEND;
79
+            }
80
+            break;
81
+
82
+        default:
83
+            state = 0;
84
+            break;
85
+    }
86
+
87
+    return 0;
88
+}
89
+
90
+void mp_master_start(void) BANKED {
91
+    game(); // TODO
92
+    state = 0;
93
+}
94
+
95
+uint8_t mp_slave_ready(void) BANKED {
96
+    switch (state) {
97
+        case MP_S_START:
98
+            SB_REG = SLAVE_HELLO;
99
+            SC_REG = SIOF_XFER_START | SIOF_CLOCK_EXT;
100
+            next_t = timer_get() + RETRANSMIT_TIME;
101
+            mp_connection_status++;
102
+            state = MP_S_WAIT;
103
+            break;
104
+
105
+        case MP_S_WAIT:
106
+            if (!(SC_REG & SIOF_XFER_START)) {
107
+                if (SB_REG == MASTER_HELLO) {
108
+                    SB_REG = MASTER_HELLO;
109
+                    SC_REG = SIOF_XFER_START | SIOF_CLOCK_INT;
110
+                    next_t = timer_get() + RETRANSMIT_TIME;
111
+                    mp_connection_status++;
112
+                    state = MP_S_REPLY;
113
+                } else {
114
+                    state = MP_S_START;
115
+                }
116
+            }
117
+            if (timer_get() >= next_t) {
118
+                state = MP_S_START;
119
+            }
120
+            break;
121
+
122
+        case MP_S_REPLY:
123
+            if (!(SC_REG & SIOF_XFER_START)) {
124
+                if (SB_REG == SLAVE_HELLO) {
125
+                    return 1;
126
+                }
127
+            }
128
+            if (timer_get() >= next_t) {
129
+                state = MP_S_START;
130
+            }
131
+            break;
132
+
133
+        default:
134
+            state = 0;
135
+            break;
136
+    }
137
+
138
+    return 0;
139
+}
140
+
141
+void mp_slave_start(void) BANKED {
142
+    game(); // TODO
143
+    state = 0;
144
+}

+ 33
- 0
src/multiplayer.h View File

@@ -0,0 +1,33 @@
1
+/*
2
+ * multiplayer.h
3
+ * Duality
4
+ *
5
+ * Copyright (C) 2025 Thomas Buck <thomas@xythobuz.de>
6
+ *
7
+ * This program 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 3 of the License, or
10
+ * (at your option) any later version.
11
+ *
12
+ * This program 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
+ * See <http://www.gnu.org/licenses/>.
18
+ */
19
+
20
+#ifndef __MULTIPLAYER_H__
21
+#define __MULTIPLAYER_H__
22
+
23
+#include <gbdk/platform.h>
24
+#include <stdint.h>
25
+
26
+uint8_t mp_master_ready(void) BANKED;
27
+void mp_master_start(void) BANKED;
28
+uint8_t mp_slave_ready(void) BANKED;
29
+void mp_slave_start(void) BANKED;
30
+
31
+extern uint8_t mp_connection_status;
32
+
33
+#endif // __MULTIPLAYER_H__

+ 24
- 0
src/window.c View File

@@ -33,6 +33,7 @@
33 33
 #include "git.h"
34 34
 #include "main.h"
35 35
 #include "gbprinter.h"
36
+#include "multiplayer.h"
36 37
 #include "window.h"
37 38
 
38 39
 #define MAX_DIGITS 7
@@ -259,6 +260,17 @@ void win_splash_draw(int32_t lowest, int32_t highest) BANKED {
259 260
     }
260 261
 }
261 262
 
263
+void win_splash_mp(void) BANKED {
264
+    /*
265
+    static uint8_t prev = 0;
266
+    if ((_cpu == CGB_TYPE) && (mp_connection_status != prev)) {
267
+        prev = mp_connection_status;
268
+        char c = (mp_connection_status & 0x01) ? 0x01 : 0x02;
269
+        str_ascii_l(&c, 1, 7, 2);
270
+    }
271
+    */
272
+}
273
+
262 274
 void win_score_clear(uint8_t is_black) BANKED {
263 275
     set_win_based(0, 0,
264 276
                   title_map_WIDTH / title_map_TILE_W, title_map_HEIGHT / title_map_TILE_H,
@@ -327,6 +339,9 @@ void win_about(void) BANKED {
327 339
         str_ascii(__DATE__, 0, 11);
328 340
         str_ascii(__TIME__, 0, 12);
329 341
 
342
+        str_ascii("MP Tx:", 14, 11);
343
+        str_ascii("Wait", 14, 12);
344
+
330 345
         str_ascii("Visit:", 0, 15);
331 346
         str_ascii("https://xythobuz.de", 0, 16);
332 347
     } else {
@@ -340,6 +355,15 @@ void win_about(void) BANKED {
340 355
     }
341 356
 }
342 357
 
358
+void win_about_mp(void) BANKED {
359
+    static uint8_t prev = 0;
360
+    if ((_cpu == CGB_TYPE) && (mp_connection_status != prev)) {
361
+        prev = mp_connection_status;
362
+        char c = (mp_connection_status & 0x01) ? 0x01 : 0x02;
363
+        str_ascii_l(&c, 1, 19, 12);
364
+    }
365
+}
366
+
343 367
 static uint8_t get_debug(char *name_buff, uint8_t i) NONBANKED {
344 368
     uint8_t n_len;
345 369
     START_ROM_BANK(BANK(main)) {

+ 2
- 0
src/window.h View File

@@ -26,10 +26,12 @@
26 26
 
27 27
 void win_init(uint8_t is_splash);
28 28
 void win_splash_draw(int32_t lowest, int32_t highest) BANKED;
29
+void win_splash_mp(void) BANKED;
29 30
 void win_score_clear(uint8_t is_black) BANKED;
30 31
 void win_score_draw(struct scores score, uint8_t off, uint8_t is_black) BANKED;
31 32
 void win_score_print(uint8_t status) BANKED;
32 33
 void win_about(void) BANKED;
34
+void win_about_mp(void) BANKED;
33 35
 void win_conf(void) BANKED;
34 36
 void win_debug(void) BANKED;
35 37
 void win_name(int32_t score) BANKED;

Loading…
Cancel
Save