Emulation of the 1970's Chip-8 machine. The emulator has 7 games that are unmodified from the original Chip-8 format.

Dependencies:   mbed

Committer:
taylorza
Date:
Sun Feb 08 01:58:57 2015 +0000
Revision:
0:bc3f11b1b41f
Chip-8 Emulator

Who changed what in which revision?

UserRevisionLine numberNew contents of line
taylorza 0:bc3f11b1b41f 1 #include "mbed.h"
taylorza 0:bc3f11b1b41f 2 #include "Bitmap1bpp.h"
taylorza 0:bc3f11b1b41f 3
taylorza 0:bc3f11b1b41f 4 Bitmap1bpp::Bitmap1bpp(uint16_t width, uint16_t height):
taylorza 0:bc3f11b1b41f 5 _width(width),
taylorza 0:bc3f11b1b41f 6 _height(height),
taylorza 0:bc3f11b1b41f 7 _stride((width >> 3) + ((width & 0x07) ? 1 : 0)),
taylorza 0:bc3f11b1b41f 8 _pBitmapData(new uint8_t[_stride * height])
taylorza 0:bc3f11b1b41f 9 {
taylorza 0:bc3f11b1b41f 10
taylorza 0:bc3f11b1b41f 11 }
taylorza 0:bc3f11b1b41f 12
taylorza 0:bc3f11b1b41f 13 Bitmap1bpp::~Bitmap1bpp()
taylorza 0:bc3f11b1b41f 14 {
taylorza 0:bc3f11b1b41f 15 if (_pBitmapData != NULL)
taylorza 0:bc3f11b1b41f 16 {
taylorza 0:bc3f11b1b41f 17 delete []_pBitmapData;
taylorza 0:bc3f11b1b41f 18 _pBitmapData = NULL;
taylorza 0:bc3f11b1b41f 19 }
taylorza 0:bc3f11b1b41f 20 }
taylorza 0:bc3f11b1b41f 21
taylorza 0:bc3f11b1b41f 22 void Bitmap1bpp::clear()
taylorza 0:bc3f11b1b41f 23 {
taylorza 0:bc3f11b1b41f 24 memset(_pBitmapData, 0, _stride * _height);
taylorza 0:bc3f11b1b41f 25 }
taylorza 0:bc3f11b1b41f 26
taylorza 0:bc3f11b1b41f 27 void Bitmap1bpp::setPixel(int16_t x, int16_t y, uint16_t color)
taylorza 0:bc3f11b1b41f 28 {
taylorza 0:bc3f11b1b41f 29 if (x < 0 || x >= _width || y < 0 || y >= _height) return;
taylorza 0:bc3f11b1b41f 30
taylorza 0:bc3f11b1b41f 31 uint8_t mask = 1 << (7 - (x % 8));
taylorza 0:bc3f11b1b41f 32 uint8_t *p = _pBitmapData + ((y * _stride) + (x / 8));
taylorza 0:bc3f11b1b41f 33
taylorza 0:bc3f11b1b41f 34 if (color) *p |= mask; else *p &= ~mask;
taylorza 0:bc3f11b1b41f 35 }
taylorza 0:bc3f11b1b41f 36
taylorza 0:bc3f11b1b41f 37 uint16_t Bitmap1bpp::getPixel(int16_t x, int16_t y)
taylorza 0:bc3f11b1b41f 38 {
taylorza 0:bc3f11b1b41f 39 if (x < 0 || x >= _width || y < 0 || y >= _height) return 0;
taylorza 0:bc3f11b1b41f 40
taylorza 0:bc3f11b1b41f 41 uint8_t mask = 1 << (7 - (x % 8));
taylorza 0:bc3f11b1b41f 42 uint8_t *p = _pBitmapData + ((y * _stride) + (x / 8));
taylorza 0:bc3f11b1b41f 43
taylorza 0:bc3f11b1b41f 44 return (*p & mask) == mask ? 1 : 0;
taylorza 0:bc3f11b1b41f 45 }
taylorza 0:bc3f11b1b41f 46
taylorza 0:bc3f11b1b41f 47 void Bitmap1bpp::fastHLine(int16_t x1, int16_t x2, int16_t y, uint16_t color)
taylorza 0:bc3f11b1b41f 48 {
taylorza 0:bc3f11b1b41f 49 for (int x = x1; x <= x2; ++x)
taylorza 0:bc3f11b1b41f 50 {
taylorza 0:bc3f11b1b41f 51 setPixel(x, y, color);
taylorza 0:bc3f11b1b41f 52 }
taylorza 0:bc3f11b1b41f 53 }
taylorza 0:bc3f11b1b41f 54
taylorza 0:bc3f11b1b41f 55 void Bitmap1bpp::fastVLine(int16_t y1, int16_t y2, int16_t x, uint16_t color)
taylorza 0:bc3f11b1b41f 56 {
taylorza 0:bc3f11b1b41f 57 for (int y = y1; y <= y2; ++y)
taylorza 0:bc3f11b1b41f 58 {
taylorza 0:bc3f11b1b41f 59 setPixel(x, y, color);
taylorza 0:bc3f11b1b41f 60 }
taylorza 0:bc3f11b1b41f 61 }