⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bsp.c

📁 事件驱动程序设计很好的框架
💻 C
字号:
/****************************************************************************** Product: "Fly'n'Shoot" game, BSP for 80x86, Vanilla/DOS, Turbo C++ 1.01* Last Updated for Version: 4.0.00* Date of the Last Update:  Apr 07, 2008**                    Q u a n t u m     L e a P s*                    ---------------------------*                    innovating embedded systems** Copyright (C) 2002-2008 Quantum Leaps, LLC. All rights reserved.** This software may be distributed and modified under the terms of the GNU* General Public License version 2 (GPL) as published by the Free Software* Foundation and appearing in the file GPL.TXT included in the packaging of* this file. Please note that GPL Section 2[b] requires that all works based* on this software must also be made publicly available under the terms of* the GPL ("Copyleft").** Alternatively, this software may be distributed and modified under the* terms of Quantum Leaps commercial licenses, which expressly supersede* the GPL and are specifically designed for licensees interested in* retaining the proprietary status of their code.** Contact information:* Quantum Leaps Web site:  http://www.quantum-leaps.com* e-mail:                  info@quantum-leaps.com*****************************************************************************/#include "qp_port.h"#include "game.h"#include "bsp.h"#include <stdlib.h>                                          /* for _exit() */Q_DEFINE_THIS_FILE/* Local-scope objects -----------------------------------------------------*/static void interrupt (*l_dosTmrISR)();static void interrupt (*l_dosKbdISR)();static void interrupt (*l_dosSpareISR)();#ifdef Q_SPY    static uint16_t l_uart_base;        /* QS data uplink UART base address */    QSTimeCtr l_tickTime;                        /* keeps timetsamp at tick */    #define UART_TXFIFO_DEPTH 16#endif#define TMR_VECTOR      0x08#define KBD_VECTOR      0x09#define SPARE_VECTOR    0x81/*..........................................................................*/static void interrupt ISR_tmr() {    static QEvent const tickEvt = { TIME_TICK_SIG, 0 };    QF_INT_UNLOCK(dummy);                              /* unlock interrupts */    QF_tick();                             /* process all armed time events */    QF_publish(&tickEvt);                         /* publish the tick event */#ifdef Q_SPY    l_tickTime += 0x10000;#endif    QF_INT_LOCK(dummy);                            /* lock interrupts again */    outportb(0x20, 0x20);                    /* write EOI to the master PIC */}/*..........................................................................*/static void interrupt ISR_kbd() {    static uint8_t ship_pos = GAME_SHIP_Y;    uint8_t key;    uint8_t kcr;    QF_INT_UNLOCK(dummy);                              /* unlock interrupts */    key = inport(0x60);           /* key scan code from 8042 kbd controller */    kcr = inport(0x61);                    /* get keyboard control register */    outportb(0x61, (uint8_t)(kcr | 0x80));   /* toggle acknowledge bit high */    outportb(0x61, kcr);                      /* toggle acknowledge bit low */    switch (key) {        case 200:                                               /* Up-arrow */        case 208: {                                           /* Down-arrow */            ObjectPosEvt *ope = Q_NEW(ObjectPosEvt, PLAYER_SHIP_MOVE_SIG);            if ((key == (uint8_t)200) && (ship_pos > 0x00)) {                --ship_pos;            }            else if ((key == (uint8_t)208)                     && (ship_pos < (GAME_SCREEN_HEIGHT - 3))) {                ++ship_pos;            }            ope->x = (uint8_t)GAME_SHIP_X;           /* x-position is fixed */            ope->y = (uint8_t)ship_pos;            QActive_postFIFO(AO_Ship, (QEvent *)ope);   /* post to the ship */            Video_printNumAt(24, 24, VIDEO_FGND_YELLOW, ship_pos);            break;        }        case 57: {                                                 /* Space */            static uint16_t ntrig = 0;            static QEvent const fireEvt = { PLAYER_TRIGGER_SIG, 0 };            QF_publish(&fireEvt);            Video_printNumAt(47, 24, VIDEO_FGND_YELLOW, ++ntrig);            break;        }                                                            /* Esc */        case 129: {            static QEvent const quitEvt = { PLAYER_QUIT_SIG, 0 };            QF_publish(&quitEvt);            break;        }    }    QF_INT_LOCK(dummy);                            /* lock interrupts again */    outportb(0x20, 0x20);                    /* write EOI to the master PIC */}/*..........................................................................*/void BSP_init(int argc, char *argv[]) {    char const *com = "COM1";    if (argc > 1) {        com = argv[1];    }    if (!QS_INIT(com)) {                                   /* initialize QS */        Q_ERROR();    }    Video_clearScreen(VIDEO_BGND_LIGHT_GRAY);    Video_clearRect( 0,  0, 80,   1, VIDEO_BGND_RED | VIDEO_BLINK);    Video_clearRect( 0,  8, 80,  24, VIDEO_BGND_BLACK | VIDEO_FGND_WHITE);    Video_clearRect( 0,  7, 80,   8, VIDEO_BGND_BLUE);    Video_clearRect( 0, 24, 80,  25, VIDEO_BGND_BLUE);    Video_clearRect(24, 24, 28,  25, VIDEO_BGND_RED | VIDEO_BLINK);    Video_clearRect(24, 24, 28,  25, VIDEO_BGND_RED | VIDEO_BLINK);    Video_printStrAt(35,  0, VIDEO_FGND_WHITE, "FLY 'n' SHOOT");    Video_printStrAt(15,  2, VIDEO_FGND_BLACK,                     "Press UP-arrow   to move the space ship up");    Video_printStrAt(15,  3, VIDEO_FGND_BLACK,                     "Press DOWN-arrow to move the space ship down");    Video_printStrAt(15,  4, VIDEO_FGND_BLACK,                     "Press SPACE      to fire the missile");    Video_printStrAt(15,  5, VIDEO_FGND_BLACK,                     "Press ESC        to quit the game");    Video_printStrAt( 8, 24, VIDEO_FGND_WHITE, "Ship Position:");    Video_printStrAt(37, 24, VIDEO_FGND_WHITE, "Triggers:");    Video_printStrAt(61, 24, VIDEO_FGND_WHITE, "Score:");    Video_clearRect(24, 24, 28,  25, VIDEO_BGND_RED);    Video_clearRect(47, 24, 51,  25, VIDEO_BGND_RED);    Video_clearRect(68, 24, 72,  25, VIDEO_BGND_RED);    Video_printNumAt(24, 24, VIDEO_FGND_YELLOW, 0);    Video_printNumAt(47, 24, VIDEO_FGND_YELLOW, 0);    Video_printNumAt(68, 24, VIDEO_FGND_YELLOW, 0);    (void)com;                  /* avoid compiler warning if QS is not used */}/*..........................................................................*/void BSP_drawBitmap(uint8_t const *bitmap, uint8_t width, uint8_t height) {    Video_drawBitmapAt(0, 8, bitmap, width, height);}/*..........................................................................*/void BSP_drawNString(uint8_t x, uint8_t y, char const *str) {    Video_drawStringAt(x, 8 + y*8, str);}/*..........................................................................*/void BSP_updateScore(uint16_t score) {    if (score == 0) {        Video_clearRect(68, 24, 72,  25, VIDEO_BGND_RED);    }    Video_printNumAt(68, 24, VIDEO_FGND_YELLOW, score);}/*..........................................................................*/void QF_onStartup(void) {    uint16_t count;                                      /* save the origingal DOS vectors ... */    l_dosTmrISR   = getvect(TMR_VECTOR);    l_dosKbdISR   = getvect(KBD_VECTOR);    l_dosSpareISR = getvect(SPARE_VECTOR);    QF_INT_LOCK(dummy);    count = (uint16_t)(((1193180 * 2) / BSP_TICKS_PER_SEC + 1) >> 1);    outportb(0x43, 0x36);             /* use mode-3 for timer 0 in the 8254 */    outportb(0x40, count & 0xFF);              /* load low  byte of timer 0 */    outportb(0x40, (count >> 8) & 0xFF);       /* load high byte of timer 0 */    setvect(TMR_VECTOR, &ISR_tmr);    setvect(KBD_VECTOR, &ISR_kbd);    setvect(SPARE_VECTOR, l_dosTmrISR);    QF_INT_UNLOCK(dummy);}/*..........................................................................*/void QF_onCleanup(void) {                               /* restore the DOS system clock tick rate... */    QF_INT_LOCK(dummy);    outportb(0x43, 0x36);             /* use mode-3 for timer 0 in the 8254 */    outportb(0x40, 0);                         /* load low  byte of timer 0 */    outportb(0x40, 0);                         /* load high byte of timer 0 */                                   /* restore the original DOS vectors ... */    setvect(TMR_VECTOR, l_dosTmrISR);    setvect(KBD_VECTOR, l_dosKbdISR);    setvect(SPARE_VECTOR, l_dosSpareISR);    QF_INT_UNLOCK(dummy);    QS_EXIT();                                                   /* exit QS */    _exit(0);                                                /* exit to DOS */}/*..........................................................................*/void QF_onIdle(void) {    QF_INT_UNLOCK(dummy);                       /* always unlock interrutps */#ifdef Q_SPY    if ((inportb(l_uart_base + 5) & (1 << 5)) != 0) {     /* Tx FIFO empty? */        uint16_t fifo = UART_TXFIFO_DEPTH;    /* depth of the 15550 Tx FIFO */        uint8_t const *block;        QF_INT_LOCK(dummy);        block = QS_getBlock(&fifo);    /* try to get next block to transmit */        QF_INT_UNLOCK(dummy);        while (fifo-- != 0) {                    /* any bytes in the block? */            outportb(l_uart_base + 0, *block++);        }    }#endif}/*--------------------------------------------------------------------------*/void Q_onAssert(char const Q_ROM * const Q_ROM_VAR file, int line) {    QF_INT_LOCK(dummy);    Video_clearRect ( 0, 24, 80, 25, VIDEO_BGND_RED);    Video_printStrAt( 0, 24, VIDEO_FGND_WHITE, "ASSERTION FAILED in file:");    Video_printStrAt(26, 24, VIDEO_FGND_YELLOW, file);    Video_printStrAt(57, 24, VIDEO_FGND_WHITE, "line:");    Video_printNumAt(62, 24, VIDEO_FGND_YELLOW, line);    QF_stop();                                       /* stop QF and cleanup */}/*--------------------------------------------------------------------------*/#ifdef Q_SPY/*..........................................................................*/static uint8_t UART_config(char const *comName, uint32_t baud) {    switch (comName[3]) {          /* Set the base address of the COMx port */        case '1': l_uart_base = (uint16_t)0x03F8; break;            /* COM1 */        case '2': l_uart_base = (uint16_t)0x02F8; break;            /* COM2 */        case '3': l_uart_base = (uint16_t)0x03E8; break;            /* COM3 */        case '4': l_uart_base = (uint16_t)0x02E8; break;            /* COM4 */        default: return (uint8_t)0;        /* COM port out of range failure */    }    baud = (uint16_t)(115200UL / baud);            /* divisor for baud rate */    outportb(l_uart_base + 3, (1 << 7));   /* Set divisor access bit (DLAB) */    outportb(l_uart_base + 0, (uint8_t)baud);               /* Load divisor */    outportb(l_uart_base + 1, (uint8_t)(baud >> 8));    outportb(l_uart_base + 3, (1 << 1) | (1 << 0));/* LCR:8-bits,no p,1stop */    outportb(l_uart_base + 4, (1 << 3) | (1 << 1) | (1 << 0));/*DTR,RTS,Out2*/    outportb(l_uart_base + 1, 0);    /* Put UART into the polling FIFO mode */    outportb(l_uart_base + 2, (1 << 2) | (1 << 0));/* FCR: enable, TX clear */    return (uint8_t)1;                                           /* success */}/*..........................................................................*/uint8_t QS_onStartup(void const *arg) {    static uint8_t qsBuf[1*1024];                 /* buffer for Quantum Spy */    QS_initBuf(qsBuf, sizeof(qsBuf));    return UART_config((char const *)arg, 115200UL);}/*..........................................................................*/void QS_onCleanup(void) {}/*..........................................................................*/QSTimeCtr QS_onGetTime(void) {            /* invoked with interrupts locked */    static uint32_t l_lastTime;    uint32_t now;    uint16_t count16;                         /* 16-bit count from the 8254 */    outportb(0x43, 0);                  /* latch the 8254's counter-0 count */    count16 = (uint16_t)inportb(0x40);    /* read the low byte of counter-0 */    count16 += ((uint16_t)inportb(0x40) << 8);        /* add on the hi byte */    now = l_tickTime + (0x10000 - count16);    if (l_lastTime > now) {                 /* are we going "back" in time? */        now += 0x10000;               /* assume that there was one rollover */    }    l_lastTime = now;    return (QSTimeCtr)now;}/*..........................................................................*/void QS_onFlush(void) {    uint16_t b;    while ((b = QS_getByte()) != QS_EOD) { /* next QS trace byte available? */        while ((inportb(l_uart_base + 5) & (1 << 5)) == 0) {  /* not empty? */        }        outportb(l_uart_base + 0, (uint8_t)b);   /* put the byte to TX FIFO */    }}#endif                                                             /* Q_SPY *//*--------------------------------------------------------------------------*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -