Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this project, you will be creating a program to play a simplified version of

ID: 3670829 • Letter: F

Question

For this project, you will be creating a program to play a simplified version of "Craps," a well-known dice game. In the game, two dice are thrown, and the players bet on the outcome of the roll (the sum of the upturned faces). The basic rules of Craps are as follows: The player makes a bet for the game by either betting for himself ("Pass Line") or against himself ("Don't Pass Line") If the player bets on the Pass Line: If the first roll of the dice results in a 7 or 11, then the player immediately wins the amount of the bet; If the first roll of the dice results in a 2, 3 or 12, then the player immediately loses the amount of the bet; If the first roll is a number other than 2, 3, 7, 11 or 12, the number that is rolled is called the player's "point." In addition, the player may increase ("press") the amount of his/her bet at this point. A For purposes of this game, the player should be allowed to double the amount of the bet. If the player does not have enough money in the bankroll to double the bet, they can still press the bet, but instead of doubling the bet, the rest of the money in the bankroll is added to the current bet. (In other words, the amount that can be "pressed" is either double the bet, or the rest of the money in the bankroll, whichever is less.) At this point, the player's goal is to roll the same "point" again *before* rolling a 7 (called "sevening out"). If the player bets on the Don't Pass Line, the result is basically a mirror image of the above. Here are the rules: If the first roll of the dice results in a 2, 3 or 12, then the player immediately wins the amount of the bet; If the first roll of the dice results in a 7 or 11, then the player immediately *loses* the amount of the bet; If the first roll is a number other than 2, 3, 7, 11 or 12, then the player who bets against him/herself is betting that s/he will roll a 7 *before* rolling the "point" a second time A If the 7 is rolled, the player wins; A if the point is rolled first, the player loses. As when betting for him/herself, the player should be allowed to double the amount of the bet if he/she wishes. (See the rules for the pass line regarding "pressing" the bet). When the player has either "sevened out" or "made the point," the game is completed, and a new game begins. A If the player won, s/he must decide whether to pick up his/her chips and quit or to play another game. Your program should give the player an initial bankroll of $100.00.A The minimum bet is $5.00. For purposes of this game, there is no maximum bet, other than the amount of money available. At the start of the program you will give an introductory message and then prompt the player to enter a random number seed. You will need to design and implement several functions in order to complete this game. You will need to decide on appropriate variables in which to store the player's bankroll (in order to keep track of how much money the player has) and how much s/he won or lost in the most recent game. This bankroll should be kept up to date on the player's current status, which means that wins should be added and losses should be subtracted from the bankroll as the user plays the game. After each game, the program must report the result of the game, the amount of money won or lost, and the current value of the bankroll. After each game, the program should allow the player to continue playing until s/he chooses to quit, or until the player runs out of money. This central program control may be done within main(). "Rolling" the dice: A separate function will be used to "roll" the dice. This function will contain two die variables. For a roll, you will set each variable separately using a random number generator. The possible values are one through six, corresponding to the six sides of a regular die. The function will print the value of each die after it is rolled (but not the total). This function will return the sum of the two variables. For help on using random() to generate random numbers, click here. A second function will be used to play a single game of craps until the player either wins or loses a bet, based upon the rules given above. This function should be passed the current $ amount of the player's bankroll and should return the amount of money won or lost for that particular game. Within the function, the player is asked how much s/he would like to bet. The player must then choose whether to bet "for" or "against" him/herself (see game rules above). The player then "rolls the dice" (simulated by a call to the dice-rolling function). This should be done interactively (via a key press from the player), rather than simply having the program continuously roll the dice until the game is over. After each roll, this function should report the total value of the dice (after receiving this value from the dice-rolling function). If, after the first roll, the game is not over, a point is established and the player should be asked whether s/he would like to double the amount of the bet (this option is given only after the first roll). When a roll causes the end of a game, the player is notified whether s/he won or lost money in that game overall, and the amount won or lost. In all other cases, the player is notified that s/he needs to roll again. At the completion of a winning game (but before the function returns), you will allow the player to "let it ride" (leave the A current bet+winnings on the table as the bet for the next game). If the player chooses to let it ride, start a new game with the current bet and winnings and using the previous Pass or Don't Pass Line as the type of bet. Otherwise, exit the function, at which point the program will return the winnings to the player's bankroll and ask if the player would like to play again. The amount returned should be used to update the player's bankroll. Additional functions may be necessary and/or appropriate, depending upon the final overall design of your program.

Explanation / Answer


#include "myLib.h"
#include "textTile.h"
#include "dice.h"
#include <stdio.h>

#define MAXWIDTH 239
#define MAXHEIGHT 159

typedef enum { TITLE, CHOOSELINE, PASSLINEBET, DONTPASSLINEBET, WIN, LOSE} STATE; //Creat Gamestates

//prototypes
void titleScreen();
void loseScreen();
void winScreen(int payOut);
void chooseLineScreen();
void passLineScreen();
void dontPassLineScreen();
int rollDice(int row, int col, int duration);
void clearMainScreen();
void clearTextScreen();

int main(void)
{
    int i;
   int diceRoll = 0;
   int diceRoll2 = 0;
   int bet = 0;
   int payOut = 0;
   int count = 0;

   u16 myPalette[] = {BLACK, WHITE, RED, GREEN, BLUE}; //Make Colors

   for(i=0; i<16384/2; i++) //Stores Text Tiles
   {
   CHARBLOCKBASE[1].tileimg[i] = fontdata_8x8[i*2] | (fontdata_8x8[i*2+1]<<8);
   }

   for(i=0; i<1600/2; i++) //Stores Dice Tiles
   {
   CHARBLOCKBASE[0].tileimg[i] = dice_8x8[i*2] | (dice_8x8[i*2+1]<<8);
   }

   for(i=0; i<5; i++) //Stores Palette Colors
   {
   PALETTE[i] = myPalette[i];
   }

    REG_DISPCTL = MODE0 | BG0_ENABLE | BG1_ENABLE;
    REG_BG0HOFS = 0;
    REG_BG0VOFS = 0;

    REG_BG0CNT = BG_SIZE0 | SBB(30) | COLOR256 | CBB(0); //Layer for Dice
    REG_BG1CNT = BG_SIZE0 | SBB(31) | COLOR256 | CBB(1); //Layer for Text

    STATE gameState = TITLE; //Sets Initial Game State

    while(1)
    {
       switch(gameState)
       {
       case TITLE:
           titleScreen();
           while(KEY_DOWN_NOW(BUTTON_START)){};
           while(!KEY_DOWN_NOW(BUTTON_START))
           {
               count++; //Time Count for Seeding
           }
           srand(count);//Random Seed
           gameState = CHOOSELINE;
           clearTextScreen();
           waitForVblank();
           break;
       case CHOOSELINE:
           chooseLineScreen();
           //Checks for Bet Type
           while(!KEY_DOWN_NOW(BUTTON_A) && !KEY_DOWN_NOW(BUTTON_B)){}
           if(KEY_DOWN_NOW(BUTTON_A))
           {
               gameState = PASSLINEBET;
           }

           if(KEY_DOWN_NOW(BUTTON_B))
           {
               gameState = DONTPASSLINEBET;
           }
           while(KEY_DOWN_NOW(BUTTON_A) || KEY_DOWN_NOW(BUTTON_B)){}
           clearTextScreen();
           waitForVblank();
           break;
       case PASSLINEBET:
           passLineScreen();
           //Checks for Bet Amount
           while(!KEY_DOWN_NOW(BUTTON_A) && !KEY_DOWN_NOW(BUTTON_B)){}

           if(KEY_DOWN_NOW(BUTTON_A))
           {
               bet = 5;
           }

           if(KEY_DOWN_NOW(BUTTON_B))
           {
               bet = 25;
           }

           while(KEY_DOWN_NOW(BUTTON_A) || KEY_DOWN_NOW(BUTTON_B)){}

           //Rolls Dice and Computes Results
           putText(12,0,"Bet Accepted");
           waitForVblank();
           putText(14,0, "Rolling");
           waitForVblank();
           diceRoll = rollDice(18,2,50);
           waitForVblank();
           if(diceRoll == 7 || diceRoll == 11)
           {
               gameState = WIN;
           }
           else if(diceRoll == 2 || diceRoll == 3 || diceRoll == 12)
           {
               gameState = LOSE;
           }
           else
           {
               putText(16,0,"Press Start to Roll Again");
               waitForVblank();
               while(!KEY_DOWN_NOW(BUTTON_START))
               {
               }
               diceRoll2 = rollDice(18,9,50);
               waitForVblank();
               if(diceRoll == diceRoll2)
               {
                   gameState = WIN;
               }
               else if(diceRoll == 7)
               {
                   gameState = LOSE;
               }
               else
               {
                   gameState = LOSE;
               }
           }
           putText(12,16,"Press Start");
           putText(14,16,"to See Result");
           waitForVblank();
           while(!KEY_DOWN_NOW(BUTTON_START))
           {
           }
           clearTextScreen();
           clearMainScreen();
           waitForVblank();
           break;
       case DONTPASSLINEBET:
           dontPassLineScreen();
           //Checks for Bet Amount
           while(!KEY_DOWN_NOW(BUTTON_A) && !KEY_DOWN_NOW(BUTTON_B)){}

           if(KEY_DOWN_NOW(BUTTON_A))
           {
               bet = 5;
           }

           if(KEY_DOWN_NOW(BUTTON_B))
           {
               bet = 25;
           }

           while(KEY_DOWN_NOW(BUTTON_A) || KEY_DOWN_NOW(BUTTON_B)){}

           //Rolls Dice and Computes Results
           putText(12,0,"Bet Accepted");
           waitForVblank();
           putText(14,0, "Rolling");
           waitForVblank();
           diceRoll = rollDice(18,2,50);
           waitForVblank();
           if(diceRoll == 7 || diceRoll == 11)
           {
               gameState = LOSE;
           }
           else if(diceRoll == 2 || diceRoll == 3 || diceRoll == 12)
           {
               gameState = WIN;
           }
           else
           {
               putText(16,0,"Press Start to Roll Again");
               waitForVblank();
               while(!KEY_DOWN_NOW(BUTTON_START))
               {
               }
               diceRoll2 = rollDice(18,9,50);
               waitForVblank();
               if(diceRoll == diceRoll2)
               {
                   gameState = LOSE;
               }
               else if(diceRoll == 7)
               {
                   gameState = WIN;
               }
               else
               {
                   gameState = WIN;
               }
           }
           putText(12,16,"Press Start");
           putText(14,16,"to See Result");
           waitForVblank();
           while(!KEY_DOWN_NOW(BUTTON_START))
           {
           }
           clearTextScreen();
           clearMainScreen();
           waitForVblank();
           break;
       case WIN:
           payOut = 2*bet;
           winScreen(payOut);
           while(KEY_DOWN_NOW(BUTTON_START)){}
           while(!KEY_DOWN_NOW(BUTTON_START))
           {
           }
           gameState = TITLE;
           clearTextScreen();
           waitForVblank();
           break;
       case LOSE:
           loseScreen();
           while(KEY_DOWN_NOW(BUTTON_START)){}
           while(!KEY_DOWN_NOW(BUTTON_START))
           {
           }
           gameState = TITLE;
           clearTextScreen();
           waitForVblank();
           break;
       }
    }

   return 0;
}

void titleScreen() //Displays Title Screen
{
   putText(5,12,"Craps");
   putText(8,7, "Push Start to Play");
}

void loseScreen() //Displays Lose Screen
{
   putText(5,8,"Your Roll Lost");
   putText(8,3, "Push Start to Play Again");
}

void winScreen(int payOut) //Displays Win Screen
{
   putText(5,8,"Your Roll Won");
   switch(payOut) //Determines Payout
   {
   case 10:
       putText(8,5, "Your Payout is $10");
       break;
   case 50:
       putText(8,5, "Your Payout is $50");
       break;
   }
   putText(10,3, "Press Start to Play Again");
}

void chooseLineScreen() //Display Bet Types
{
   putText(5,11,"Press A");
   putText(7,3, "To Place a Pass Line Bet");
   putText(10,11,"Press B");
   putText(12,0, "To Place a Don't Pass Line Bet");
}

void clearTextScreen() //Erases Text
{
   int i;
   for(i=0; i < 1024; i++)
   {
       SCREENBLOCKBASE[31].tilemap[i] = 0;
   }
}

void clearMainScreen() //Erases Dice
{
   int i;
   for(i=0; i < 1024; i++)
   {
       SCREENBLOCKBASE[30].tilemap[i] = 0;
   }
}

void passLineScreen() //Displays Bets for Pass Lines
{
   putText(0,3,"Press A");
   putText(2,3, "To Place a $5 Bet");
   putText(5,3,"Press B");
   putText(7,3, "To Place a $25 Bet");
}
void dontPassLineScreen() //Displays Bets for Don't Pass Lines
{
   putText(0,3,"Press A");
   putText(2,3, "To Place a $5 Bet");
   putText(5,3,"Press B");
   putText(7,3, "To Place a $25 Bet");
}


int rollDice(int row, int col, int duration) //Rolls and Animates Dice
{
   int n;
   int d1;
   int d2;
   for(n = 0; n < duration; n++)
   {
       drawDice(row, col, rand()%6+1);
       drawDice(row, col+3, rand()%6+1);
       waitForVblank();
   }
   d1 = rand()%6+1;
   d2 = rand()%6+1;
   drawDice(row, col, d1);
   drawDice(row, col+3, d2);
   waitForVblank();
   return d1 + d2;
}

dice.c

#include "dice.h"
#include "myLib.h"

void drawDice(int row, int col, int diceNumber) //Draws Dice
{
   int start = row*32+col;
   int d = diceNumber;
   switch(d)
   {
   case 1:
       d = 1;
       break;
   case 2:
       d = 5;
       break;
   case 3:
       d = 9;
       break;
   case 4:
       d = 13;
       break;
   case 5:
       d = 17;
       break;
   case 6:
       d = 21;
       break;
   }
   SCREENBLOCKBASE[30].tilemap[start] = d;
   SCREENBLOCKBASE[30].tilemap[start+1] = d+1;
   SCREENBLOCKBASE[30].tilemap[start+32] = d+2;
   SCREENBLOCKBASE[30].tilemap[start+33] = d+3;
}

const unsigned char dice_8x8[1600] =
{
   0, 0, 0, 0, 0, 0, 0, 0,   //Background Tile
   0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0,
   // tile 0
   0, 0, 0, 0, 0, 0, 0, 0, //Tile 1-4: Dice 1
   0, 1, 1, 1, 1, 1, 1, 1,
   0, 1, 1, 1, 1, 1, 1, 1,
   0, 1, 1, 1, 1, 1, 1, 1,
   0, 1, 1, 1, 1, 1, 1, 1,
   0, 1, 1, 1, 1, 1, 1, 1,
   0, 1, 1, 1, 1, 1, 0, 0,
   0, 1, 1, 1, 1, 1, 0, 0,
   // tile 1
   0, 0, 0, 0, 0, 0, 0, 0,
   1, 1, 1, 1, 1, 1, 1, 0,
   1, 1, 1, 1, 1, 1, 1, 0,
   1, 1, 1, 1, 1, 1, 1, 0,
   1, 1, 1, 1, 1, 1, 1, 0,
   1, 1, 1, 1, 1, 1, 1, 0,
   0, 0, 1, 1, 1, 1, 1, 0,
   0, 0, 1, 1, 1, 1, 1, 0,
   // tile 2
   0, 1, 1, 1, 1, 1, 0, 0,
   0, 1, 1, 1, 1, 1, 0, 0,
   0, 1, 1, 1, 1, 1, 1, 1,
   0, 1, 1, 1, 1, 1, 1, 1,
   0, 1, 1, 1, 1, 1, 1, 1,
   0, 1, 1, 1, 1, 1, 1, 1,
   0, 1, 1, 1, 1, 1, 1, 1,
   0, 0, 0, 0, 0, 0, 0, 0,
   // tile 3
   0, 0, 1, 1, 1, 1, 1, 0,
   0, 0, 1, 1, 1, 1, 1, 0,
   1, 1, 1, 1, 1, 1, 1, 0,
   1, 1, 1, 1, 1, 1, 1, 0,
   1, 1, 1, 1, 1, 1, 1, 0,
   1, 1, 1, 1, 1, 1, 1, 0,
   1, 1, 1, 1, 1, 1, 1, 0,
   0, 0, 0, 0, 0, 0, 0, 0,
   // tile 4
   0, 0, 0, 0, 0, 0, 0, 0, //Tiles 5-8: Dice 2
   0, 1, 1, 1, 1, 1, 1, 1,
   0, 1, 1, 1, 1, 1, 1, 1,
   0, 1, 1, 1, 1, 1, 1, 1,
   0, 1, 1, 1, 1, 1, 1, 1,
   0, 1, 1, 1, 1, 1, 1, 1,
   0, 1, 1, 1, 1, 1, 1, 1,
   0, 1, 1, 1, 1, 1, 1, 1,
};

dice.h
/*********************************************************************
* dice.c
********************************************************************/

#include "dice.h"
#include "myLib.h"

void drawDice(int row, int col, int diceNumber) //Draws Dice
{
   int start = row*32+col;
   int d = diceNumber;
   switch(d)
   {
   case 1:
       d = 1;
       break;
   case 2:
       d = 5;
       break;
   case 3:
       d = 9;
       break;
   case 4:
       d = 13;
       break;
   case 5:
       d = 17;
       break;
   case 6:
       d = 21;
       break;
   }
   SCREENBLOCKBASE[30].tilemap[start] = d;
   SCREENBLOCKBASE[30].tilemap[start+1] = d+1;
   SCREENBLOCKBASE[30].tilemap[start+32] = d+2;
   SCREENBLOCKBASE[30].tilemap[start+33] = d+3;
}

const unsigned char dice_8x8[1600] =
{
   0, 0, 0, 0, 0, 0, 0, 0,   //Background Tile
   0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0,
   // tile 0
   0, 0, 0, 0, 0, 0, 0, 0, //Tile 1-4: Dice 1
   0, 1, 1, 1, 1, 1, 1, 1,
   0, 1, 1, 1, 1, 1, 1, 1,
   0, 1, 1, 1, 1, 1, 1, 1,
   0, 1, 1, 1, 1, 1, 1, 1,
   0, 1, 1, 1, 1, 1, 1, 1,
   0, 1, 1, 1, 1, 1, 0, 0,
   0, 1, 1, 1, 1, 1, 0, 0,
   // tile 1
   0, 0, 0, 0, 0, 0, 0, 0,
   1, 1, 1, 1, 1, 1, 1, 0,
   1, 1, 1, 1, 1, 1, 1, 0,
   1, 1, 1, 1, 1, 1, 1, 0,
   1, 1, 1, 1, 1, 1, 1, 0,
   1, 1, 1, 1, 1, 1, 1, 0,
   0, 0, 1, 1, 1, 1, 1, 0,
   0, 0, 1, 1, 1, 1, 1, 0,
   // tile 2
   0, 1, 1, 1, 1, 1, 0, 0,
   0, 1, 1, 1, 1, 1, 0, 0,
   0, 1, 1, 1, 1, 1, 1, 1,
   0, 1, 1, 1, 1, 1, 1, 1,
   0, 1, 1, 1, 1, 1, 1, 1,
   0, 1, 1, 1, 1, 1, 1, 1,
   0, 1, 1, 1, 1, 1, 1, 1,
   0, 0, 0, 0, 0, 0, 0, 0,
   // tile 3
   0, 0, 1, 1, 1, 1, 1, 0,
   0, 0, 1, 1, 1, 1, 1, 0,
   1, 1, 1, 1, 1, 1, 1, 0,
   1, 1, 1, 1, 1, 1, 1, 0,
   1, 1, 1, 1, 1, 1, 1, 0,
   1, 1, 1, 1, 1, 1, 1, 0,
   1, 1, 1, 1, 1, 1, 1, 0,
   0, 0, 0, 0, 0, 0, 0, 0,
   // tile 4
   0, 0, 0, 0, 0, 0, 0, 0, //Tiles 5-8: Dice 2
   0, 1, 1, 1, 1, 1, 1, 1,
   0, 1, 1, 1, 1, 1, 1, 1,
   0, 1, 1, 1, 1, 1, 1, 1,
   0, 1, 1, 1, 1, 1, 1, 1,
   0, 1, 1, 1, 1, 1, 1, 1,
   0, 1, 1, 1, 1, 1, 1, 1,
   0, 1, 1, 1, 1, 1, 1, 1,
};

myLib.c
/*-------------------------------------------------------------------
*
* Library for general GBA programming
*
*-----------------------------------------------------------------*/
#include "myLib.h"

#define USE_DMA

unsigned short *videoBuffer = VIDBUFADR;

void setPixel3(int row, int col, unsigned short color)
{
   videoBuffer[OFFSET(row,col)] = color;
}

#ifndef USE_DMA

// Plain version i.e. No DMA
void drawRect3(int row, int col, int height, int width, volatile unsigned short color)
{
   int r, c;
   for(r=0; r<height; r++)
   {
       for(c=0; c<width; c++)
       {
           setPixel3(row+r, col+c, color);
       }
   }
}
#else

// DMA Version
void drawRect3(int row, int col, int height, int width, volatile unsigned short color)
{
   int r;
   for(r=0; r<height; r++)
   {
       DMA[3].src = &color;
       DMA[3].dst = videoBuffer + OFFSET(r+row, col);
       DMA[3].cnt = width | DMA_SOURCE_FIXED | DMA_ON;
   }
}
#endif

void setPixel4(int row, int col, unsigned char index)
{
   int whichPixel = OFFSET(row,col);
   int whichShort = whichPixel/2;
   if(col & 1) // Is it odd?
   {
       videoBuffer[whichShort] = (videoBuffer[whichShort] & 0x00FF) | (index << 8);
   }
   else
   {
       // even column
       videoBuffer[whichShort] = (videoBuffer[whichShort] & 0xFF00) | (index);
   }
}

void drawRect4OLD(int row, int col, int height, int width, volatile unsigned char index)
{
   int r, c;
   for(r=0; r<height; r++)
   {
       for(c=0; c<width; c++)
       {
           setPixel4(row+r, col+c, index);
       }
   }
}

// Draw Rectangle using DMA
// This version always rectangles that start on even columns and are an even number
// of pixels wide
void drawRect4(int row, int col, int height, int width, volatile unsigned char index)
{
   volatile u16 indexindex = (index << 8) | index;
   int r;
   col = col/2*2;
   for(r=0; r<height; r++)
   {
       DMA[3].src = &indexindex;
       DMA[3].dst = videoBuffer + OFFSET(r+row, col)/2;
       DMA[3].cnt = (width/2) | DMA_SOURCE_FIXED | DMA_ON;

   }
}

void waitForVblank()
{
   while(SCANLINECOUNTER > 160)
   {

   }
   while(SCANLINECOUNTER < 160)
   {

   }
}

void fillScreen3(volatile unsigned short color)
{
   volatile unsigned int colorcolor = color<<16 | color;
   DMA[3].src = &colorcolor;
   DMA[3].dst = videoBuffer;
   DMA[3].cnt = 19200 | DMA_SOURCE_FIXED | DMA_32 | DMA_ON;

}

void copyPicture4(const unsigned short *buffer)
{

   DMA[3].src = buffer;
   DMA[3].dst = videoBuffer;
   DMA[3].cnt = 19200 | DMA_16 | DMA_ON;

}

void FlipPage()
{
   if(REG_DISPCTL & BUFFER1FLAG) // Is it BUFFER1
   {
       // YES
       REG_DISPCTL = REG_DISPCTL & ~BUFFER1FLAG;
       videoBuffer = BUFFER1;
   }
   else
   {
       // NO
       REG_DISPCTL = REG_DISPCTL | BUFFER1FLAG;
       videoBuffer = BUFFER0;
   }
}

myLib.h

/*-------------------------------------------------------------------
*
* myLib.h -- Header File for general GBA programming
*
*------------------------------------------------------------------*/
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;

// Useful addresses
#define REG_DISPCTL (*(u16 *)0x4000000)
#define SCANLINECOUNTER (*(volatile unsigned short *)0x4000006)
#define VIDBUFADR ((unsigned short *)0x6000000)

// Generally useful symbols
#define FOREVER 1
#define TRUE 1
#define FALSE 0

// Video
#define MODE3 3
#define BG0_ENABLE (1<<8)
#define BG1_ENABLE (1<<9)
#define BG2_ENABLE (1<<10)
#define BG3_ENABLE (1<<11)

#define OFFSET(r,c) ((r)*240+(c))

extern unsigned short *videoBuffer;

// Colors

#define COLOR(r,g,b) ((r) | (g)<<5 | (b)<<10)

#define WHITE COLOR(31,31,31)
#define BLACK 0
#define RED COLOR(31,0,0)
#define GREEN COLOR(0,31,0)
#define BLUE COLOR(0,0,31)
#define YELLOW COLOR(31,31,0)
#define CYAN COLOR(0,31,31)
#define MAGENTA COLOR(31,0,31)
#define LTGRAY COLOR(25, 25, 25)
// Prototypes
void setPixel3(int, int, unsigned short);
void drawRect3(int row, int col, int height, int width, volatile unsigned short color);
void waitForVblank();
void fillScreen3(volatile unsigned short color);
void setPixel4(int row, int col, unsigned char index);
void drawRect4(int row, int col, int height, int width, volatile unsigned char index);
void FlipPage();
void copyPicture4(const unsigned short *buffer);

// *** Input =========================================================

// Buttons

#define BUTTONS (*(volatile unsigned int *)0x04000130)

#define BUTTON_A 1
#define BUTTON_B 2
#define BUTTON_SELECT 4
#define BUTTON_START 8
#define BUTTON_RIGHT 16
#define BUTTON_LEFT 32
#define BUTTON_UP 64
#define BUTTON_DOWN 128
#define BUTTON_R 256
#define BUTTON_L 512

#define BUTTON_INDEX_A 0
#define BUTTON_INDEX_B 1
#define BUTTON_INDEX_SELECT 2
#define BUTTON_INDEX_START 3
#define BUTTON_INDEX_RIGHT 4
#define BUTTON_INDEX_LEFT 5
#define BUTTON_INDEX_UP 6
#define BUTTON_INDEX_DOWN 7
#define BUTTON_INDEX_R 8
#define BUTTON_INDEX_L 9

#define KEY_DOWN_NOW(key) (~BUTTONS & key)

/* DMA */

typedef struct
{                               // ***********************************************************
   const volatile void *src;   // We mark this as const which means we can assign it const
   volatile void *dst;       // things without the compiler yelling but we can also pass it
   volatile u32 cnt;           // things that are not const!
                               // ***********************************************************
} DMAREC;

#define DMA ((volatile DMAREC *)0x040000B0)

#define DMA_DESTINATION_INCREMENT (0 << 21)
#define DMA_DESTINATION_DECREMENT (1 << 21)
#define DMA_DESTINATION_FIXED (2 << 21)
#define DMA_DESTINATION_RESET (3 << 21)

#define DMA_SOURCE_INCREMENT (0 << 23)
#define DMA_SOURCE_DECREMENT (1 << 23)
#define DMA_SOURCE_FIXED (2 << 23)

#define DMA_REPEAT (1 << 25)

#define DMA_16 (0 << 26)
#define DMA_32 (1 << 26)

#define DMA_NOW (0 << 28)
#define DMA_AT_VBLANK (1 << 28)
#define DMA_AT_HBLANK (2 << 28)
#define DMA_AT_REFRESH (3 << 28)

#define DMA_IRQ (1 << 30)
#define DMA_ON (1 << 31)

#define START_ON_FIFO_EMPTY 0x30000000

// Mode 4
#define MODE4 4
#define PALETTE ((u16 *)0x5000000)


#define BUFFER0 ((u16 *)0x6000000)
#define BUFFER1 ((u16 *)0x600A000)

#define BUFFER1FLAG (1<<4)

// Mode 0


#define MODE0 0

typedef struct
{
    u16 tileimg[8192];

} charblock;

typedef struct
{
    u16 tilemap[1024];

} screenblock;

//extern charblock *charblockBase;
//extern screenblock *screenblockBase;

#define CHARBLOCKBASE ((charblock *)0x6000000)
#define SCREENBLOCKBASE ((screenblock *)0x6000000)

//background offset registers
#define REG_BG0HOFS *(volatile unsigned short *)0x04000010
#define REG_BG1HOFS *(volatile unsigned short *)0x04000014
#define REG_BG2HOFS *(volatile unsigned short *)0x04000018
#define REG_BG3HOFS *(volatile unsigned short *)0x0400001C

#define REG_BG0VOFS *(volatile unsigned short *)0x04000012
#define REG_BG1VOFS *(volatile unsigned short *)0x04000016
#define REG_BG2VOFS *(volatile unsigned short *)0x0400001A
#define REG_BG3VOFS *(volatile unsigned short *)0x0400001E
//background control registers
#define REG_BG0CNT *(volatile unsigned short*)0x4000008
#define REG_BG1CNT *(volatile unsigned short*)0x400000A
#define REG_BG2CNT *(volatile unsigned short*)0x400000C
#define REG_BG3CNT *(volatile unsigned short*)0x400000E

    //macros and bit constants for setting the background control register specifics
#define SBB(num) ((num) << 8)
#define CBB(num) ((num) << 2)

#define COLOR256 1 << 7
                            // Cols x Row
#define BG_SIZE0 0<<14      // 32 x 32 tiles
#define BG_SIZE1 1<<14      // 64 x 32
#define BG_SIZE2 2<<14      // 32 x 64
#define BG_SIZE3 3<<14      // 64 x 64

textTile.h

/*********************************************************************
*
* texttile.h
*
********************************************************************/

extern const unsigned char fontdata_8x8[16384];

void putText(int row, int col, char *buffer);

textTile.c

/*********************************************************************
* textTile.c
********************************************************************/


#include "textTile.h"
#include "myLib.h"

void putText(int row, int col, char *buffer) //Places Text on Screen
{
   int start = row*32+col;
   while(*buffer != '')
   {
       SCREENBLOCKBASE[31].tilemap[start] = *buffer;
       start++;
       buffer++;
   }
}


const unsigned char fontdata_8x8[16384] = {
/* num: 0 */
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
/* num: 1 */
0,0,1,1,1,0,0,0,
0,1,0,0,0,1,0,0,
0,1,1,0,1,1,0,0,
0,1,0,0,0,1,0,0,
0,1,0,1,0,1,0,0,
0,1,0,0,0,1,0,0,
0,0,1,1,1,0,0,0,
0,0,0,0,0,0,0,0,
/* num: 2 */
0,0,1,1,1,0,0,0,
0,1,1,1,1,1,0,0,
0,1,0,1,0,1,0,0,
0,1,1,1,1,1,0,0,
0,1,0,0,0,1,0,0,
0,1,1,1,1,1,0,0,
0,0,1,1,1,0,0,0,
0,0,0,0,0,0,0,0,
/* num: 3 */
0,0,0,0,0,0,0,0,
0,0,1,0,1,0,0,0,
0,1,1,1,1,1,0,0,
0,1,1,1,1,1,0,0,
0,1,1,1,1,1,0,0,
0,0,1,1,1,0,0,0,
0,0,0,1,0,0,0,0,
0,0,0,0,0,0,0,0,
/* num: 4 */
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,1,0,1,0,0,0,
0,0,1,1,1,0,0,0,
0,0,1,1,1,0,0,0,
0,0,0,1,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,

};

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote