From ad5d763a86533eab14914aa1e683575ae6b3cfce Mon Sep 17 00:00:00 2001 From: David McMackins II Date: Sat, 13 May 2017 13:46:24 -0500 Subject: Change to new Delwink style --- README | 6 +-- stdgb.c | 139 ++++++++++++++++++++++++++++++++-------------------------------- stdgb.h | 50 +++++++++++------------ 3 files changed, 95 insertions(+), 100 deletions(-) diff --git a/README b/README index 046f0a1..f35ffb3 100644 --- a/README +++ b/README @@ -46,11 +46,7 @@ assembly language. This is necessary for a few reasons: Hacking ------- -libstdgb uses the GNU coding style for C with a strict 79-column limit in the -source code. For "hot" operations, assembly code is permitted, but make sure to -heavily comment its functionality. The goal here is to make this program as -accessible to C programmers as possible while maintaining binary size and -execution time efficiency. +Match the style you see in the code. Do not use spaces for indentation. If you would like to contribute, [send your diff to Delwink][1], or use GitHub's pull request system. diff --git a/stdgb.c b/stdgb.c index 7b37f3f..87f4241 100644 --- a/stdgb.c +++ b/stdgb.c @@ -1,7 +1,7 @@ /* * libstdgb - library of useful Game Boy operations - * Copyright (C) 2016 Delwink, LLC - * + * Copyright (C) 2016-2017 Delwink, LLC + * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, version 3 only. @@ -22,126 +22,125 @@ static uint8_t * const DPAD_STATE = (void *) 0xDFA0; static uint8_t * const BTN_STATE = (void *) 0xDFA1; void -gb_mbc5_select_rombank (uint16_t bank) +gb_mbc5_select_rombank(uint16_t bank) { - uint8_t upper = (bank & 0xFF00) >> 8; - uint8_t lower = bank & 0x00FF; + uint8_t upper = (bank & 0xFF00) >> 8; + uint8_t lower = bank & 0x00FF; - *((uint8_t *) 0x3000) = upper; - *((uint8_t *) 0x2000) = lower; + *((uint8_t *) 0x3000) = upper; + *((uint8_t *) 0x2000) = lower; } static void -invert_state (uint8_t *state) +invert_state(uint8_t *state) { - *state = (~(*state)) & 0x0F; + *state = (~(*state)) & 0x0F; } void -gb_update_input_state () +gb_update_input_state() { - // this routine is copied from the official Nintendo programming manual - __asm__ ("ld a, #0x20\n\t" // load flag to read d-pad input - "ld (#0xFF00), a\n\t" // make input request - "ld a, (#0xFF00)\n\t" // read input ports - "ld a, (#0xFF00)\n\t" // do this twice for necessary delay - "ld (#0xDFA0), a\n\t" // save it in *DPAD_STATE - - "ld a, #0x10\n\t" // load flag to read button input - "ld (#0xFF00), a\n\t" // make input request - "ld a, (#0xFF00)\n\t" // read input ports - "ld a, (#0xFF00)\n\t" // do this six times for necessary delay - "ld a, (#0xFF00)\n\t" - "ld a, (#0xFF00)\n\t" - "ld a, (#0xFF00)\n\t" - "ld a, (#0xFF00)\n\t" - "ld (#0xDFA1), a\n\t" // save it in *BTN_STATE - - "ld a, #0x30\n\t" // load flag to read nothing - "ld (#0xFF00), a"); // disable input reads - - invert_state (DPAD_STATE); - invert_state (BTN_STATE); + // this routine is copied from the official Nintendo programming manual + __asm__("ld a, #0x20\n\t" // load flag to read d-pad input + "ld (#0xFF00), a\n\t" // make input request + "ld a, (#0xFF00)\n\t" // read input ports + "ld a, (#0xFF00)\n\t" // do this twice for necessary delay + "ld (#0xDFA0), a\n\t" // save it in *DPAD_STATE + + "ld a, #0x10\n\t" // load flag to read button input + "ld (#0xFF00), a\n\t" // make input request + "ld a, (#0xFF00)\n\t" // read input ports + "ld a, (#0xFF00)\n\t" // do this six times for necessary delay + "ld a, (#0xFF00)\n\t" + "ld a, (#0xFF00)\n\t" + "ld a, (#0xFF00)\n\t" + "ld a, (#0xFF00)\n\t" + "ld (#0xDFA1), a\n\t" // save it in *BTN_STATE + + "ld a, #0x30\n\t" // load flag to read nothing + "ld (#0xFF00), a"); // disable input reads + + invert_state(DPAD_STATE); + invert_state(BTN_STATE); } uint8_t -gb_dpad_down (uint8_t direction) +gb_dpad_down(uint8_t direction) { - return *DPAD_STATE & direction; + return *DPAD_STATE & direction; } uint8_t -gb_button_down (uint8_t button) +gb_button_down(uint8_t button) { - return *BTN_STATE & button; + return *BTN_STATE & button; } void -gb_enable_vblank () +gb_enable_vblank() { - *GB_INT_ENABLE |= GB_INT_VBLANK; - gb_enable_interrupts (); + *GB_INT_ENABLE |= GB_INT_VBLANK; + gb_enable_interrupts(); } void -gb_wait_vblank () +gb_wait_vblank() { - do - { - gb_halt (); - } - while (!gb_have_vblank ()); + do + { + gb_halt(); + } while (!gb_have_vblank()); } void -gb_define_tile (uint8_t start, const uint8_t *data) +gb_define_tile(uint8_t start, const uint8_t *data) { - uint8_t *tiles = GB_TILE_DATA; - gb_memcpy (&tiles[start * GB_BYTES_PER_TILE], data, GB_BYTES_PER_TILE); + uint8_t *tiles = GB_TILE_DATA; + gb_memcpy(&tiles[start * GB_BYTES_PER_TILE], data, GB_BYTES_PER_TILE); } void -gb_set_view (uint8_t x, uint8_t y) +gb_set_view(uint8_t x, uint8_t y) { - *GB_SCROLL_X = x; - *GB_SCROLL_Y = y; + *GB_SCROLL_X = x; + *GB_SCROLL_Y = y; } void -gb_shift_view (int8_t x, int8_t y) +gb_shift_view(int8_t x, int8_t y) { - *GB_SCROLL_X += x; - *GB_SCROLL_Y += y; + *GB_SCROLL_X += x; + *GB_SCROLL_Y += y; } static void -copy_objects () +copy_objects() { - __asm__ ("ld a,#0xDF\n\t" // specify that objects are at 0xDF00 - "ld (0xFF46),a\n\t" // start DMA copy - "ld a,#0x28\n" // set wait counter to 40 - "obj_copy_wait:\n\t" - "dec a\n\t" // decrement counter - "jr nz,obj_copy_wait"); // continue to wait until counter is zero + __asm__("ld a,#0xDF\n\t" // specify that objects are at 0xDF00 + "ld (0xFF46),a\n\t" // start DMA copy + "ld a,#0x28\n" // set wait counter to 40 + "obj_copy_wait:\n\t" + "dec a\n\t" // decrement counter + "jr nz,obj_copy_wait"); // wait until counter is zero } void -gb_init_objects () +gb_init_objects() { - gb_memcpy (GB_HRAM, (uint8_t *) copy_objects, 12); - memset (GB_OBJECTS, 0, GB_NUM_OBJECTS * GB_BYTES_PER_OBJ); - gb_update_objects (); + gb_memcpy(GB_HRAM, (uint8_t *) copy_objects, 12); + memset(GB_OBJECTS, 0, GB_NUM_OBJECTS * GB_BYTES_PER_OBJ); + gb_update_objects(); } void -gb_update_objects () +gb_update_objects() { - __asm__ ("jp 0xFF80"); // real function is at HRAM + __asm__("jp 0xFF80"); // real function is at HRAM } void -gb_memcpy (uint8_t *dest, const uint8_t *src, uint8_t n) +gb_memcpy(uint8_t *dest, const uint8_t *src, uint8_t n) { - while (n--) - *(dest++) = *(src++); + while (n--) + *(dest++) = *(src++); } diff --git a/stdgb.h b/stdgb.h index 544fca1..5c4b12b 100644 --- a/stdgb.h +++ b/stdgb.h @@ -1,7 +1,7 @@ /* * libstdgb - library of useful Game Boy operations - * Copyright (C) 2016 Delwink, LLC - * + * Copyright (C) 2016-2017 Delwink, LLC + * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, version 3 only. @@ -106,13 +106,13 @@ #define GB_BTN_A (0x01) void -gb_update_input_state (void); +gb_update_input_state(void); uint8_t -gb_dpad_down (uint8_t direction); +gb_dpad_down(uint8_t direction); uint8_t -gb_button_down (uint8_t button); +gb_button_down(uint8_t button); /* MBC1 controls */ @@ -124,14 +124,14 @@ gb_button_down (uint8_t button); /* MBC5 controls */ void -gb_mbc5_select_rombank (uint16_t bank); +gb_mbc5_select_rombank(uint16_t bank); /* generic controls */ -#define gb_disable_interrupts() __asm__ ("di"); -#define gb_enable_interrupts() __asm__ ("ei"); -#define gb_halt() __asm__ ("halt\n\tnop"); -#define gb_stop() __asm__ ("stop"); +#define gb_disable_interrupts() __asm__("di"); +#define gb_enable_interrupts() __asm__("ei"); +#define gb_halt() __asm__("halt\n\tnop"); +#define gb_stop() __asm__("stop"); #define gb_enable_rambank() *((uint8_t *) 0x0000) = 0x0A; #define gb_disable_rambank() *((uint8_t *) 0x0000) = 0x00; @@ -198,23 +198,23 @@ gb_mbc5_select_rombank (uint16_t bank); #define GB_OBJ_PAL1 (0x10) void -gb_set_view (uint8_t x, uint8_t y); +gb_set_view(uint8_t x, uint8_t y); void -gb_shift_view (int8_t x, int8_t y); +gb_shift_view(int8_t x, int8_t y); #define gb_have_vblank() ((*GB_LCD_STATE & 0x03) == 1) void -gb_enable_vblank (void); +gb_enable_vblank(void); void -gb_wait_vblank (void); +gb_wait_vblank(void); #define GB_BYTES_PER_TILE (16) void -gb_define_tile (uint8_t i, const uint8_t *tile_data); +gb_define_tile(uint8_t i, const uint8_t *tile_data); #define gb_set_lcd_mode(M) *GB_LCD = (M); @@ -225,21 +225,21 @@ gb_define_tile (uint8_t i, const uint8_t *tile_data); #define GB_NUM_OBJECTS (40) enum _gb_sprite_model - { - GB_OBJ_YPOS = 0, - GB_OBJ_XPOS, - GB_OBJ_TILE, - GB_OBJ_FLAGS, - GB_BYTES_PER_OBJ - }; +{ + GB_OBJ_YPOS = 0, + GB_OBJ_XPOS, + GB_OBJ_TILE, + GB_OBJ_FLAGS, + GB_BYTES_PER_OBJ +}; extern uint8_t (* const GB_OBJECTS)[GB_BYTES_PER_OBJ]; void -gb_init_objects (void); +gb_init_objects(void); void -gb_update_objects (void); +gb_update_objects(void); #define gb_set_object_palette(I,P) GB_OBJ_PALETTES[(I)] = (P); @@ -277,6 +277,6 @@ gb_update_objects (void); /* generic functions */ void -gb_memcpy (uint8_t *dest, const uint8_t *src, uint8_t n); +gb_memcpy(uint8_t *dest, const uint8_t *src, uint8_t n); #endif -- cgit v1.2.3