Facebook
From bonebone, 2 Weeks ago, written in C.
Embed
Download Paste or View Raw
Hits: 113
  1. // ============================ //
  2. // Do not edit this part!!!!    //
  3. // ============================ //
  4. // 0x300001 - CONFIG1H
  5. #pragma config OSC = HSPLL // Oscillator Selection bits (HS oscillator,
  6.                            // PLL enabled (Clock Frequency = 4 x FOSC1))
  7. #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit
  8.                            // (Fail-Safe Clock Monitor disabled)
  9. #pragma config IESO = OFF  // Internal/External Oscillator Switchover bit
  10.                            // (Oscillator Switchover mode disabled)
  11. // 0x300002 - CONFIG2L
  12. #pragma config PWRT = OFF  // Power-up Timer Enable bit (PWRT disabled)
  13. #pragma config BOREN = OFF // Brown-out Reset Enable bits (Brown-out
  14.                            // Reset disabled in hardware and software)
  15. // 0x300003 - CONFIG1H
  16. #pragma config WDT = OFF // Watchdog Timer Enable bit
  17.                          // (WDT disabled (control is placed on the SWDTEN bit))
  18. // 0x300004 - CONFIG3L
  19. // 0x300005 - CONFIG3H
  20. #pragma config LPT1OSC = OFF // Low-Power Timer1 Oscillator Enable bit
  21.                              // (Timer1 configured for higher power operation)
  22. #pragma config MCLRE = ON    // MCLR Pin Enable bit (MCLR pin enabled;
  23.                              // RE3 input pin disabled)
  24. // 0x300006 - CONFIG4L
  25. #pragma config LVP = OFF   // Single-Supply ICSP Enable bit (Single-Supply
  26.                            // ICSP disabled)
  27. #pragma config XINST = OFF // Extended Instruction Set Enable bit
  28.                            // (Instruction set extension and Indexed
  29.                            // Addressing mode disabled (Legacy mode))
  30.  
  31. #pragma config DEBUG = OFF // Disable In-Circuit Debugger
  32.  
  33. #define KHZ 1000UL
  34. #define MHZ (KHZ * KHZ)
  35. #define _XTAL_FREQ (40UL * MHZ)
  36.  
  37. // ============================ //
  38. //             End              //
  39. // ============================ //
  40.  
  41. #include <xc.h>
  42. #include <stdint.h>
  43.  
  44. #define T_PRELOAD_LOW 0xC1
  45. #define T_PRELOAD_HIGH 0x34
  46.  
  47. // Hold the board as an array of 8x4:
  48. // 0 0 0 0
  49. // 0 0 0 0
  50. // 0 0 0 0
  51. // 0 0 0 0
  52. // 0 0 0 0
  53. // 0 0 0 0
  54. // 0 0 0 0
  55. // 0 0 0 0
  56. uint8_t board[8][4];
  57. // Initialize the board
  58. void init_board()
  59. {
  60.     for (uint8_t i = 0; i < 8; i++)
  61.     {
  62.         for (uint8_t j = 0; j < 4; j++)
  63.         {
  64.             board[i][j] = 0;
  65.         }
  66.     }
  67. }
  68.  
  69. // Submit a piece to board.
  70. void submit_piece(struct piece)
  71. {
  72.     // Before submitting the piece, check if the piece can be submitted.
  73.     // If the piece cannot be submitted, return.
  74.     if (piece->type == dot)
  75.     {
  76.         if (board[piece->x][piece->y] == 1)
  77.         {
  78.             return;
  79.         }
  80.     }
  81.     else if (piece->type == l_piece)
  82.     {
  83.         if (piece->empty_x == piece->x && piece->empty_y == piece->y)
  84.         {
  85.             // Empty space is on the top-left.
  86.             // OX
  87.             // XX
  88.             if (board[piece->x + 1][piece->y] == 1 || board[piece->x][piece->y + 1] == 1 || board[piece->x + 1][piece->y + 1] == 1)
  89.             {
  90.                 return;
  91.             }
  92.         }
  93.         else if (piece->empty_x < piece->x && piece->empty_y == piece->y)
  94.         {
  95.             // Empty space is on the top-right.
  96.             // XO
  97.             // XX
  98.             if (board[piece->x][piece->y] == 1 || board[piece->x][piece->y + 1] == 1 || board[piece->x + 1][piece->y + 1] == 1)
  99.             {
  100.                 return;
  101.             }
  102.         }
  103.         else if (piece->x < piece->empty_x && piece->x < piece->empty_y)
  104.         {
  105.             // Empty space is on the bottom-right.
  106.             // XX
  107.             // XO
  108.             if (board[piece->x][piece->y] == 1 || board[piece->x + 1][piece->y] == 1 || board[piece->x][piece->y + 1] == 1)
  109.             {
  110.                 return;
  111.             }
  112.         }
  113.         else if (piece->empty_x == piece->x + 1)
  114.         {
  115.             // Empty space is on the bottom-left.
  116.             // XX
  117.             // OX
  118.             if (board[piece->x][piece->y] == 1 || board[piece->x + 1][piece->y] == 1 || board[piece->x + 1][piece->y + 1] == 1)
  119.             {
  120.                 return;
  121.             }
  122.         }
  123.     }
  124.     else if (piece->type == square)
  125.     {
  126.         if (board[piece->x][piece->y] == 1 || board[piece->x][piece->y + 1] == 1 || board[piece->x + 1][piece->y] == 1 || board[piece->x + 1][piece->y + 1] == 1)
  127.         {
  128.             return;
  129.         }
  130.     }
  131.     // Set correct values to the board as 1:
  132.     if (piece->type == "dot")
  133.     {
  134.         board[piece->x][piece->y] = 1;
  135.     }
  136.     else if (piece->type == "l_piece")
  137.     {
  138.         if (piece->empty_x == piece->x && piece->empty_y == piece->y)
  139.         {
  140.             // Empty space is on the top-left.
  141.             // OX
  142.             // XX
  143.             board[piece->x + 1][piece->y] = 1;
  144.             board[piece->x][piece->y + 1] = 1;
  145.             board[piece->x + 1][piece->y + 1] = 1;
  146.         }
  147.         else if (piece->empty_x < piece->x && piece->empty_y == piece->y)
  148.         {
  149.             // Empty space is on the top-right.
  150.             // XO
  151.             // XX
  152.             board[piece->x][piece->y] = 1;
  153.             board[piece->x][piece->y + 1] = 1;
  154.             board[piece->x + 1][piece->y + 1] = 1;
  155.         }
  156.         else if (piece->x < piece->empty_x && piece->x < piece->empty_y)
  157.         {
  158.             // Empty space is on the bottom-right.
  159.             // XX
  160.             // XO
  161.             board[piece->x][piece->y] = 1;
  162.             board[piece->x + 1][piece->y] = 1;
  163.             board[piece->x][piece->y + 1] = 1;
  164.         }
  165.         else if (piece->empty_x == piece->x + 1)
  166.         {
  167.             // Empty space is on the bottom-left.
  168.             // XX
  169.             // OX
  170.             board[piece->x][piece->y] = 1;
  171.             board[piece->x + 1][piece->y] = 1;
  172.             board[piece->x + 1][piece->y + 1] = 1;
  173.         }
  174.     }
  175.     else if (piece->type == "square")
  176.     {
  177.         board[piece->x][piece->y] = 1;
  178.         board[piece->x][piece->y + 1] = 1;
  179.         board[piece->x + 1][piece->y] = 1;
  180.         board[piece->x + 1][piece->y + 1] = 1;
  181.     }
  182. }
  183.  
  184. // Add a structure for pieces
  185. struct piece
  186. {
  187.     uint8_t x;
  188.     uint8_t y;
  189.     uint8_t empty_x;
  190.     uint8_t empty_y;
  191.     // Type of the piece: "dot", "l_piece", "square"
  192.     char *type;
  193. };
  194. // Square
  195. struct square
  196. {
  197.     uint8_t x;
  198.     uint8_t y;
  199. };
  200.  
  201. // Add a helper function to rotate the L-Piece
  202. void rotate_l_piece(struct piece *piece)
  203. {
  204.     // Pieces always rotate clockwise.
  205.     // Within given square space, determine the next position of empty space.
  206.     // Depending on the current position of the piece, move the empty space to the next position.
  207.     if (piece->empty_x == piece->x && piece->empty_y == piece->y)
  208.     {
  209.         // Empty space is on the top-left.
  210.         // OX -> XO
  211.         // XX -> XX
  212.         piece->empty_x = piece->x + 1;
  213.     }
  214.     else if (piece->empty_x < piece->x && piece->empty_y == piece->y)
  215.     {
  216.         // Empty space is on the top-right.
  217.         // XO -> XX
  218.         // XX -> XO
  219.         piece->empty_y = piece->y + 1;
  220.     }
  221.     else if (piece->empty_y == piece->y)
  222.     {
  223.         // Empty space is on the bottom-right.
  224.         // XX -> XX
  225.         // XO -> OX
  226.         piece->empty_x = piece->x - 1;
  227.     }
  228.     else if (piece->empty_x == piece->x + 1)
  229.     {
  230.         // Empty space is on the bottom-left.
  231.         // XX -> OX
  232.         // OX -> XX
  233.         piece->empty_y = piece->y - 1;
  234.     }
  235. }
  236.  
  237. // Add helper functions to move pieces.
  238.  
  239. void move_piece_left(struct piece *piece)
  240. {
  241.     if (piece->x == 0)
  242.     {
  243.         return;
  244.     }
  245.     piece->x--;
  246.     piece->empty_x--;
  247. }
  248. void move_piece_right(struct piece *piece)
  249. {
  250.     if (piece->x == 6)
  251.     {
  252.         return;
  253.     }
  254.     piece->x++;
  255.     piece->empty_x++;
  256. }
  257. void move_piece_down(struct piece *piece)
  258. {
  259.     if (piece->y == 6)
  260.     {
  261.         return;
  262.     }
  263.     piece->y++;
  264.     piece->empty_y++;
  265. }
  266. void move_piece_up(struct piece *piece)
  267. {
  268.     if (piece->y == 0)
  269.     {
  270.         return;
  271.     }
  272.     piece->y--;
  273.     piece->empty_y--;
  274. }
  275.  
  276. uint8_t prevB;
  277.  
  278. void Init()
  279. {
  280.     // B
  281.     LATB = 0x00;
  282.     PORTB = 0x00;
  283.     TRISB = 0x10;
  284.     // C
  285.     LATC = 0x00;
  286.     PORTC = 0x00;
  287.     TRISC = 0x00;
  288.     // D
  289.     LATD = 0x00;
  290.     PORTD = 0x00;
  291.     TRISD = 0x00;
  292. }
  293.  
  294. void InitializeTimerAndInterrupts()
  295. {
  296.     // Enable pre-scalar
  297.     // Full pre-scale
  298.     // we also need to do in-code scaling
  299.     T0CON = 0x00;
  300.     T0CONbits.TMR0ON = 1;
  301.     T0CONbits.T0PS2 = 1;
  302.     T0CONbits.T0PS1 = 0;
  303.     T0CONbits.T0PS0 = 1;
  304.     // Pre-load the value
  305.     TMR0H = T_PRELOAD_HIGH;
  306.     TMR0L = T_PRELOAD_LOW;
  307.  
  308.     RCONbits.IPEN = 0;
  309.     INTCON = 0x00;
  310.     INTCONbits.TMR0IE = 1;
  311.     INTCONbits.RBIE = 1;
  312.     INTCONbits.GIE = 1;
  313.     INTCONbits.PEIE = 1;
  314. }
  315.  
  316. // ============================ //
  317. //   INTERRUPT SERVICE ROUTINE  //
  318. // ============================ //
  319. __interrupt(high_priority) void HandleInterrupt()
  320. {
  321.     // Timer overflowed (333 ms)
  322.     if (INTCONbits.TMR0IF)
  323.     {
  324.         INTCONbits.TMR0IF = 0;
  325.         // Pre-load the value
  326.         TMR0H = T_PRELOAD_HIGH;
  327.         TMR0L = T_PRELOAD_LOW;
  328.  
  329.         // Fully lit LATC
  330.         LATD = ~PORTD;
  331.     }
  332.     if (INTCONbits.RBIF)
  333.     {
  334.         // Read the value to satisfy the interrupt
  335.         prevB = PORTB;
  336.  
  337.         LATC = ~PORTC;
  338.  
  339.         // Then clear the bit
  340.         INTCONbits.RBIF = 0;
  341.     }
  342. }
  343.  
  344. __interrupt(low_priority) void HandleInterrupt2()
  345. {
  346. }
  347. // ============================ //
  348. //            MAIN              //
  349. // ============================ //
  350. void main()
  351. {
  352.     Init();
  353.     InitializeTimerAndInterrupts();
  354.  
  355.     prevB = PORTB;
  356.     __delay_ms(100);
  357.  
  358.     // Main Loop
  359.     while (1)
  360.     {
  361.         //...
  362.     }
  363. }