diff --git a/display.c b/display.c index a715e87..e6c8d2b 100644 --- a/display.c +++ b/display.c @@ -7,25 +7,25 @@ uint16_t rscreenWidth, rscreenHeight; // Real screen size uint16_t displayXOffset; // Margin of the display area -uint32_t pix_buffer[SCREEN_REAL_WIDTH]; +uint32_t pix_buffer[GE0_SCREEN_WIDTH]; void setScreenResolution(uint16_t nw, uint16_t nh) { - if (nw < SCREEN_REAL_WIDTH) + if (nw < GE0_SCREEN_WIDTH) rscreenWidth = nw; else - rscreenWidth = SCREEN_REAL_WIDTH - 1; + rscreenWidth = GE0_SCREEN_WIDTH - 1; - if (nh < SCREEN_REAL_HEIGHT) + if (nh < GE0_SCREEN_HEIGHT) rscreenHeight = nh; else - rscreenHeight = SCREEN_REAL_HEIGHT - 1; + rscreenHeight = GE0_SCREEN_HEIGHT - 1; if (rscreenWidth < 95) rscreenWidth = 95; if (rscreenHeight < 95) rscreenHeight = 95; - displayXOffset = (SCREEN_REAL_WIDTH - rscreenWidth) / 2; + displayXOffset = (GE0_SCREEN_WIDTH - rscreenWidth) / 2; for (int i = 0; i < 4; i++) line_is_draw[i] = 0xffffffff; ge0_port_display_fillScreen(0x0000); @@ -35,8 +35,8 @@ void redrawScreen() { // todo: change the name to display. "screen" is for // canvas only // left shift so we can use fix-point number // use (_ * _ratio) >> 16 for mapping - int x_ratio = (int)((SCREEN_WIDTH << 16) / rscreenWidth); - int y_ratio = (int)((SCREEN_HEIGHT << 16) / rscreenHeight); + int x_ratio = (int)((GE0_SCREEN_HEIGHT << 16) / rscreenWidth); + int y_ratio = (int)((GE0_SCREEN_HEIGHT << 16) / rscreenHeight); int x_canvas, hx2, y_canvas, startx; // Since each line in canvas might be mapped to mutiple // lines in display. This could reduce the compution need for redraw diff --git a/include/display.h b/include/display.h index c12cce6..57150d5 100644 --- a/include/display.h +++ b/include/display.h @@ -3,11 +3,11 @@ #include -// todo: change this two -// this two in orig project keep the real hw size of the screen -// while the render size is configurable -#define SCREEN_REAL_WIDTH 128 -#define SCREEN_REAL_HEIGHT 128 +// // todo: change this two +// // this two in orig project keep the real hw size of the screen +// // while the render size is configurable +// #define GE0_SCREEN_WIDTH 128 +// #define GE0_SCREEN_HEIGHT 128 void setScreenResolution(uint16_t nw, uint16_t nh); void redrawScreen(void); diff --git a/include/screen.h b/include/screen.h index d05a323..28e1dc5 100644 --- a/include/screen.h +++ b/include/screen.h @@ -3,15 +3,16 @@ #include #include +#include "utils.h" +// #define SCREEN_HEIGHT 128 +// #define SCREEN_WIDTH 128 -#define SCREEN_HEIGHT 128 -#define SCREEN_WIDTH 128 -#define SCREEN_WIDTH_BYTES 64 +#define SCREEN_WIDTH_BYTES (GE0_SCREEN_WIDTH * 4 / 8) // Each pixel usse 4 bit +#define SCREEN_WIDTH_BYTES_LOG BITS_TO_REPRESENT(SCREEN_WIDTH_BYTES) +#define SCREEN_SIZE (SCREEN_WIDTH_BYTES * GE0_SCREEN_HEIGHT) +#define SCREEN_ARRAY_DEF SCREEN_SIZE -#define SCREEN_SIZE (SCREEN_HEIGHT * SCREEN_WIDTH_BYTES) -#define SCREEN_ARRAY_DEF SCREEN_SIZE - -#define SCREEN_ADDR(x, y) (((y) << 6) + (x)) +#define SCREEN_ADDR(x, y) (((y) << SCREEN_WIDTH_BYTES_LOG) + (x)) struct sprite { uint16_t address; diff --git a/include/utils.h b/include/utils.h new file mode 100644 index 0000000..182a68b --- /dev/null +++ b/include/utils.h @@ -0,0 +1,45 @@ +#ifndef UTILS_H_ +#define UTILS_H_ + +#define IS_REPRESENTIBLE_IN_D_BITS(D, N) \ + (((unsigned long) N >= (1UL << (D - 1)) && (unsigned long) N < (1UL << D)) ? D : -1) +// see https://stackoverflow.com/questions/27581671/how-to-compute-log-with-the-preprocessor +#define BITS_TO_REPRESENT(N) \ + (N == 0 ? 1 : (31 \ + + IS_REPRESENTIBLE_IN_D_BITS( 1, N) \ + + IS_REPRESENTIBLE_IN_D_BITS( 2, N) \ + + IS_REPRESENTIBLE_IN_D_BITS( 3, N) \ + + IS_REPRESENTIBLE_IN_D_BITS( 4, N) \ + + IS_REPRESENTIBLE_IN_D_BITS( 5, N) \ + + IS_REPRESENTIBLE_IN_D_BITS( 6, N) \ + + IS_REPRESENTIBLE_IN_D_BITS( 7, N) \ + + IS_REPRESENTIBLE_IN_D_BITS( 8, N) \ + + IS_REPRESENTIBLE_IN_D_BITS( 9, N) \ + + IS_REPRESENTIBLE_IN_D_BITS(10, N) \ + + IS_REPRESENTIBLE_IN_D_BITS(11, N) \ + + IS_REPRESENTIBLE_IN_D_BITS(12, N) \ + + IS_REPRESENTIBLE_IN_D_BITS(13, N) \ + + IS_REPRESENTIBLE_IN_D_BITS(14, N) \ + + IS_REPRESENTIBLE_IN_D_BITS(15, N) \ + + IS_REPRESENTIBLE_IN_D_BITS(16, N) \ + + IS_REPRESENTIBLE_IN_D_BITS(17, N) \ + + IS_REPRESENTIBLE_IN_D_BITS(18, N) \ + + IS_REPRESENTIBLE_IN_D_BITS(19, N) \ + + IS_REPRESENTIBLE_IN_D_BITS(20, N) \ + + IS_REPRESENTIBLE_IN_D_BITS(21, N) \ + + IS_REPRESENTIBLE_IN_D_BITS(22, N) \ + + IS_REPRESENTIBLE_IN_D_BITS(23, N) \ + + IS_REPRESENTIBLE_IN_D_BITS(24, N) \ + + IS_REPRESENTIBLE_IN_D_BITS(25, N) \ + + IS_REPRESENTIBLE_IN_D_BITS(26, N) \ + + IS_REPRESENTIBLE_IN_D_BITS(27, N) \ + + IS_REPRESENTIBLE_IN_D_BITS(28, N) \ + + IS_REPRESENTIBLE_IN_D_BITS(29, N) \ + + IS_REPRESENTIBLE_IN_D_BITS(30, N) \ + + IS_REPRESENTIBLE_IN_D_BITS(31, N) \ + + IS_REPRESENTIBLE_IN_D_BITS(32, N) \ + ) \ + ) + + +#endif // UTILS_H_ \ No newline at end of file diff --git a/mem.c b/mem.c index 25da986..1c79d10 100644 --- a/mem.c +++ b/mem.c @@ -1,23 +1,25 @@ -// // 在我搞清楚mem到底放了什么东西之前,我先不实现这几个函数了 -// int16_t readInt(uint16_t adr) { -// printf("readInt is called\n"); -// (void)adr; -// return 0; -// /* int16_t n; */ -// /* int8_t *nPtr; */ -// /* if(adr < RAM_SIZE - 1){ */ -// /* nPtr = (int8_t*)&n; */ -// /* *nPtr = lge_mem[adr++]; */ -// /* nPtr++; */ -// /* *nPtr = lge_mem[adr]; */ -// /* return n; */ -// /* } */ -// /* return 0; */ -// } +#include -// uint8_t readMem(uint16_t adr) { -// printf("readMem is called\n"); -// (void)adr; -// return 0; -// /* return (adr < RAM_SIZE) ? lge_mem[adr] : 0; */ -// } +// 在我搞清楚mem到底放了什么东西之前,我先不实现这几个函数了 +int16_t readInt(uint16_t adr) { + printf("readInt is called\n"); + (void)adr; + return 0; + /* int16_t n; */ + /* int8_t *nPtr; */ + /* if(adr < RAM_SIZE - 1){ */ + /* nPtr = (int8_t*)&n; */ + /* *nPtr = lge_mem[adr++]; */ + /* nPtr++; */ + /* *nPtr = lge_mem[adr]; */ + /* return n; */ + /* } */ + /* return 0; */ +} + +uint8_t readMem(uint16_t adr) { + printf("readMem is called\n"); + (void)adr; + return 0; + /* return (adr < RAM_SIZE) ? lge_mem[adr] : 0; */ +} diff --git a/ports/bare_metal_lcd_port.c b/ports/bare_metal_lcd_port.c index f68161f..95cbe34 100644 --- a/ports/bare_metal_lcd_port.c +++ b/ports/bare_metal_lcd_port.c @@ -1,13 +1,16 @@ #include "ge0_port_interface.h" +#include "lcd.h" void ge0_port_display_fillScreen(uint16_t color){ - + lcd_fill(0, 0, LCD_W, LCD_H, color); } void ge0_port_display_drawLine(uint32_t line, uint32_t start, uint32_t width, uint32_t *colors){ - + for(uint32_t i=0;i> 1; orig = screen[SCREEN_ADDR(x_index, y)]; if (x & 1) @@ -73,8 +73,8 @@ void setPix(uint16_t x, uint16_t y, uint8_t p) { } void clearScr(uint8_t p) { - for (uint16_t y = 0; y < SCREEN_HEIGHT; y++) { - for (uint16_t x = 0; x < SCREEN_WIDTH; x++) { + for (uint16_t y = 0; y < GE0_SCREEN_HEIGHT; y++) { + for (uint16_t x = 0; x < GE0_SCREEN_WIDTH; x++) { setPix(x, y, p); } } @@ -85,8 +85,8 @@ void clearScr(uint8_t p) { void changePalette(uint8_t n, uint16_t c) { if (n < 16) { palette[n] = c; - for (uint8_t y = 0; y < 128; y++) { - for (uint8_t x = 0; x < 64; x++) { // todo: Why x here is 64? + for (uint8_t y = 0; y < GE0_SCREEN_HEIGHT; y++) { + for (uint8_t x = 0; x < SCREEN_WIDTH_BYTES; x++) { if (((screen[SCREEN_ADDR(x, y)] & 0xf0) >> 4) == n || (screen[SCREEN_ADDR(x, y)] & 0x0f) == n) SET_LINE_IS_DRAW(y);