git.y1.nz

gbdk-2020

GameBoy Development Kit
download: https://git.y1.nz/archives/gbdk.tar.gz
README | Files | Log | Refs | LICENSE

gbdk-lib/examples/gb/comm/comm.c

      1 #include <gb/gb.h>
      2 #include <stdint.h>
      3 #include <stdio.h>
      4 
      5 unsigned char str[] = "Hello World!";
      6 unsigned char buffer[32];
      7 
      8 void main(void)
      9 {
     10     uint8_t i, n = 0;
     11     unsigned char *s;
     12 
     13     puts("Byte");
     14     puts("  A      : Send");
     15     puts("  B      : Receive");
     16     puts("String");
     17     puts("  START  : Send");
     18     puts("  SELECT : Receive");
     19 
     20     CRITICAL {
     21         add_SIO(nowait_int_handler);    // disable waiting VRAM state before return
     22     }
     23     set_interrupts(SIO_IFLAG);          // disable other interrupts. note: this disables sprite movement
     24 
     25     while(1) {
     26         i = waitpad(J_A | J_B | J_START | J_SELECT);
     27         waitpadup();
     28 
     29         if (i == J_A) {
     30             /* Send 1 byte */
     31             printf("Sending b... ");
     32             _io_out = n++;
     33             send_byte();
     34             /* Wait for IO completion... */
     35             while((_io_status == IO_SENDING) && (joypad() == 0));
     36             if(_io_status == IO_IDLE)
     37                 printf("OK\n");
     38             else
     39                 printf("#%d\n", _io_status);
     40         } else if (i == J_B) {
     41             /* Receive 1 byte */
     42             printf("Receiving b... ");
     43             receive_byte();
     44             /* Wait for IO completion... */
     45             while((_io_status == IO_RECEIVING) && (joypad() == 0));
     46             if(_io_status == IO_IDLE)
     47                 printf("OK\n%d\n", _io_in);
     48             else
     49                 printf("#%d\n", _io_status);
     50         } else if (i == J_START) {
     51             /* Send a string */
     52             printf("Sending s... ");
     53             s = str;
     54             while(1) {
     55                 _io_out = *s;
     56                 do {
     57                     send_byte();
     58                     /* Wait for IO completion... */
     59                     while((_io_status == IO_SENDING) && (joypad() == 0));
     60                 } while((_io_status != IO_IDLE) && (joypad() == 0));
     61                 if(_io_status != IO_IDLE) {
     62                     printf("#%d\n", _io_status);
     63                     break;
     64                 }
     65                 if(*s == 0) break;
     66                 s++;
     67             }
     68             if(_io_status == IO_IDLE) printf("OK\n");
     69         } else if (i == J_SELECT) {
     70             /* Receive a string */
     71             printf("Receiving s... ");
     72             s = buffer;
     73             while(1) {
     74                 receive_byte();
     75                 /* Wait for IO completion... */
     76                 while((_io_status == IO_RECEIVING) && (joypad() == 0));
     77                 if(_io_status != IO_IDLE) {
     78                     printf("#%d\n", _io_status); 
     79                     break;
     80                 }
     81                 *s = _io_in;
     82                 if(*s == 0) break;
     83                 s++;
     84             }
     85             if(_io_status == IO_IDLE) printf("OK\n%s\n", buffer);
     86         }
     87         /* In case of user cancellation */
     88         waitpadup();
     89     }
     90 }

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.