From 087d7120fed09d5816406828396dd081f7bc2db8 Mon Sep 17 00:00:00 2001 From: Gabriel Valky Date: Thu, 5 Aug 2021 23:04:05 +0200 Subject: [PATCH] Gabuino flappy bird, melody player, init_array fix --- .../examples/{gabo.cpp => demo.cpp} | 2 +- .../117_gabuino/examples/flappybird.cpp | 48 +++++ .../117_gabuino/examples/map.cpp | 10 - .../117_gabuino/examples/melody.cpp | 98 ++++++++++ .../117_gabuino/examples/return.cpp | 6 - .../apps_featured/117_gabuino/source/memory.h | 2 +- .../117_gabuino/web/v1/compiler.js | 40 +++- .../117_gabuino/web/v1/dsym_gabuino.js | 166 ++++++++-------- .../117_gabuino/web/v1/dsym_os.js | 96 ++++----- .../117_gabuino/web/v1/examples.js | 155 ++++++++++++++- .../117_gabuino/web/v1/index.html | 182 +++++++++++++++++- 11 files changed, 634 insertions(+), 171 deletions(-) rename system/apps_featured/117_gabuino/examples/{gabo.cpp => demo.cpp} (98%) create mode 100644 system/apps_featured/117_gabuino/examples/flappybird.cpp delete mode 100644 system/apps_featured/117_gabuino/examples/map.cpp create mode 100644 system/apps_featured/117_gabuino/examples/melody.cpp delete mode 100644 system/apps_featured/117_gabuino/examples/return.cpp diff --git a/system/apps_featured/117_gabuino/examples/gabo.cpp b/system/apps_featured/117_gabuino/examples/demo.cpp similarity index 98% rename from system/apps_featured/117_gabuino/examples/gabo.cpp rename to system/apps_featured/117_gabuino/examples/demo.cpp index 905961bc..f60ca492 100644 --- a/system/apps_featured/117_gabuino/examples/gabo.cpp +++ b/system/apps_featured/117_gabuino/examples/demo.cpp @@ -19,7 +19,7 @@ int main(void) y = y1; if ((c++ % 1000) == 0) - DBG::Print("%d lines, ", c/1000); + DBG::Print("%d lines, ", c); } return 0; diff --git a/system/apps_featured/117_gabuino/examples/flappybird.cpp b/system/apps_featured/117_gabuino/examples/flappybird.cpp new file mode 100644 index 00000000..d188b67b --- /dev/null +++ b/system/apps_featured/117_gabuino/examples/flappybird.cpp @@ -0,0 +1,48 @@ +// Play flappy bird in browser, press F1 on LA104 to jump +// Based on Tiny flappy bird by Daniel Bark: +// https://github.com/danba340/tiny-flappy-bird + +#include + +int main(void) +{ + BIOS::DBG::Print(R"()"); + + BIOS::DBG::Print(R"()"); + + BIOS::KEY::EKey key; + while ((key = BIOS::KEY::GetKey()) != BIOS::KEY::EKey::Escape) + { + if (key == BIOS::KEY::EKey::Enter) + BIOS::DBG::Print(R"()"); + } + return 0; +} diff --git a/system/apps_featured/117_gabuino/examples/map.cpp b/system/apps_featured/117_gabuino/examples/map.cpp deleted file mode 100644 index 873bd5b7..00000000 --- a/system/apps_featured/117_gabuino/examples/map.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include - -int main(void) -{ - BIOS::DBG::Print("
"); - BIOS::DBG::Print("
"); - BIOS::DBG::Print("
On top
"); - BIOS::DBG::Print("
"); - return 0; -} diff --git a/system/apps_featured/117_gabuino/examples/melody.cpp b/system/apps_featured/117_gabuino/examples/melody.cpp new file mode 100644 index 00000000..240aed2c --- /dev/null +++ b/system/apps_featured/117_gabuino/examples/melody.cpp @@ -0,0 +1,98 @@ +// plays Axel F on internal piezo speaker of LA104 +#include +#include + +void WriteTim8(int reg, uint16_t val) +{ + *((uint16_t*)(0x40013400 + reg)) = val; +} + +void SoundOn() +{ + WriteTim8(0x20, 0x3000); + WriteTim8(0x00, 0x0081); + WriteTim8(0x40, 150); // volume +} + +void SoundOff() +{ + WriteTim8(0x20, 0x0000); + WriteTim8(0x00, 0x0080); +} + +void Sound(int f) +{ + WriteTim8(0x24, 0); + WriteTim8(0x28, 15-1); + int div = 72e6/15/f; + WriteTim8(0x2c, div-1); + //BIOS::DBG::Print("f=%d, div=%d, ", f, div); + SoundOn(); +} + +int C=0, Cis=1, D=2, Dis=3, E=4, F=5, Fis=6, G=7, Gis=8, A=9, Ais=10, B=11, None=12; +const int BPM = 120; + +const int Beat = 60000 / BPM; +int Whole = Beat*4; +int Half = Whole/2; +int Quarter = Whole/4; +int Eigth = Whole/8; +int Sixteenth = Whole/16; +int Thirtysecond = Whole/32; +int WholeDot = Whole*1.5; +int HalfDot = Half*1.5; +int QuarterDot = Quarter*1.5; +int EigthDot = Eigth*1.5; +int SixteenthDot = Sixteenth*1.5; +int ThirtysecondDot = Thirtysecond*1.5; + +int Frequency(int note, int octave) +{ + const int table[] = {4186, 4434, 4698, 4978, 5274, 5587, 5915, 6271, 6644, 7040, 7458, 7902}; + return table[note] >> (4-octave); +} + +void Play(int note, int octave, int length) +{ + if (note != None) + Sound(Frequency(note, octave)); + + //BIOS::DBG::Print("len=%d, ", length); + BIOS::SYS::DelayMs(length); + SoundOff(); + BIOS::SYS::DelayMs(10); +} + +int main() +{ + // generated from http://x.valky.eu/klavirgen + Play(G, 2, Quarter); + Play(Ais, 2, EigthDot); + Play(G, 2, Sixteenth); + Play(None, 0, Sixteenth); + Play(G, 2, Sixteenth); + Play(C, 3, Eigth); + Play(G, 2, Eigth); + Play(F, 2, Eigth); + Play(G, 2, Quarter); + Play(D, 3, EigthDot); + Play(G, 2, Sixteenth); + Play(None, 0, Sixteenth); + Play(G, 2, Sixteenth); + Play(Dis, 3, Eigth); + Play(D, 3, Eigth); + Play(Ais, 2, Eigth); + Play(G, 2, Eigth); + Play(D, 3, Eigth); + Play(G, 3, Eigth); + Play(G, 2, Sixteenth); + Play(F, 2, Sixteenth); + Play(None, 0, Sixteenth); + Play(F, 2, Sixteenth); + Play(D, 2, Eigth); + Play(A, 2, Eigth); + Play(G, 2, Half); + + return 0; +} diff --git a/system/apps_featured/117_gabuino/examples/return.cpp b/system/apps_featured/117_gabuino/examples/return.cpp deleted file mode 100644 index 2e9fe8ac..00000000 --- a/system/apps_featured/117_gabuino/examples/return.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include - -int main(void) -{ - return 724; -} diff --git a/system/apps_featured/117_gabuino/source/memory.h b/system/apps_featured/117_gabuino/source/memory.h index 189fd678..f6b05229 100644 --- a/system/apps_featured/117_gabuino/source/memory.h +++ b/system/apps_featured/117_gabuino/source/memory.h @@ -65,7 +65,7 @@ namespace MEMORY int Exec(uint32_t ptr) { - _ASSERT(ptr == 0x20005001); + _ASSERT((ptr & 1) && (ptr >= 0x20005001)); userRetVal = 0x66667777; typedef int(*TFunc)(); diff --git a/system/apps_featured/117_gabuino/web/v1/compiler.js b/system/apps_featured/117_gabuino/web/v1/compiler.js index 4f1eccb1..192974e6 100644 --- a/system/apps_featured/117_gabuino/web/v1/compiler.js +++ b/system/apps_featured/117_gabuino/web/v1/compiler.js @@ -37,6 +37,19 @@ function writeDword(ofs, val) globalBlob[ofs-globalOffset+0] = val >> 0; } +function readDword(ofs) +{ + var t = 0; + t = globalBlob[ofs-globalOffset+3]; + t <<= 8; + t |= globalBlob[ofs-globalOffset+2]; + t <<= 8; + t |= globalBlob[ofs-globalOffset+1]; + t <<= 8; + t |= globalBlob[ofs-globalOffset+0]; + return t; +} + function clearBss(blob, ofs, len) { for (var i=0; i resolve() ) - // we do not know if the program keeps running or has ended with return code +{ + console.log("globalInit"); + var prepare = Promise.resolve(); + if (globalInit.length > 0) + { + prepare = globalInit.reduce((p, x) => p.then(_ => BIOS.exec(x|1)), Promise.resolve()) +// for (var i=0; i BIOS.exec(globalOffset|1) )//.then( () => resolve() ) + // we do not know if the program keeps running or has ended with return code return Promise.resolve(); } @@ -80,6 +102,7 @@ function stop() var globalOffset; var globalBlob; +var globalInit; var globalResolve = []; function processElf(elf) { @@ -96,6 +119,8 @@ function processElf(elf) throw "too big blob"; // TODO: BSS!!!! var appblob = new Uint8Array(end-begin); + globalBlob = appblob; + var init = []; for (var i=0; i\n' + + 'demo.cpp': '#include \n' + '\n' + 'using namespace BIOS;\n' + '\n' + @@ -239,7 +239,7 @@ examples = ' y = y1;\n' + '\n' + ' if ((c++ % 1000) == 0)\n' + - ' DBG::Print("%d lines, ", c/1000);\n' + + ' DBG::Print("%d lines, ", c);\n' + ' }\n' + ' \n' + ' return 0;\n' + @@ -404,6 +404,54 @@ examples = '}\n' + '\n' + '\n', + 'flappybird.cpp': '// Play flappy bird in browser, press F1 on LA104 to jump\n' + + '// Based on Tiny flappy bird by Daniel Bark:\n' + + '// https://github.com/danba340/tiny-flappy-bird\n' + + '\n' + + '#include \n' + + '\n' + + 'int main(void)\n' + + '{\n' + + ' BIOS::DBG::Print(R"()");\n' + + '\n' + + ' BIOS::DBG::Print(R"()");\n' + + '\n' + + ' BIOS::KEY::EKey key;\n' + + ' while ((key = BIOS::KEY::GetKey()) != BIOS::KEY::EKey::Escape)\n' + + ' {\n' + + ' if (key == BIOS::KEY::EKey::Enter)\n' + + ' BIOS::DBG::Print(R"()");\n' + + ' }\n' + + ' return 0;\n' + + '}\n', 'gps.cpp': '#include \n' + '\n' + 'using namespace BIOS;\n' + @@ -599,15 +647,102 @@ examples = ' \n' + ' return 0;\n' + '}', - 'map.cpp': '#include \n' + + 'melody.cpp': '// plays Axel F on internal piezo speaker of LA104\n' + + '#include \n' + + '#include \n' + '\n' + - 'int main(void)\n' + + 'void WriteTim8(int reg, uint16_t val)\n' + + '{\n' + + ' *((uint16_t*)(0x40013400 + reg)) = val;\n' + + '}\n' + + '\n' + + 'void SoundOn()\n' + + '{\n' + + ' WriteTim8(0x20, 0x3000);\n' + + ' WriteTim8(0x00, 0x0081);\n' + + ' WriteTim8(0x40, 150); // volume\n' + + '}\n' + + '\n' + + 'void SoundOff()\n' + + '{\n' + + ' WriteTim8(0x20, 0x0000);\n' + + ' WriteTim8(0x00, 0x0080);\n' + + '}\n' + + '\n' + + 'void Sound(int f)\n' + + '{\n' + + ' WriteTim8(0x24, 0);\n' + + ' WriteTim8(0x28, 15-1);\n' + + ' int div = 72e6/15/f;\n' + + ' WriteTim8(0x2c, div-1);\n' + + ' //BIOS::DBG::Print("f=%d, div=%d, ", f, div);\n' + + ' SoundOn();\n' + + '}\n' + + '\n' + + 'int C=0, Cis=1, D=2, Dis=3, E=4, F=5, Fis=6, G=7, Gis=8, A=9, Ais=10, B=11, None=12;\n' + + 'const int BPM = 120;\n' + + '\n' + + 'const int Beat = 60000 / BPM;\n' + + 'int Whole = Beat*4;\n' + + 'int Half = Whole/2;\n' + + 'int Quarter = Whole/4;\n' + + 'int Eigth = Whole/8;\n' + + 'int Sixteenth = Whole/16;\n' + + 'int Thirtysecond = Whole/32;\n' + + 'int WholeDot = Whole*1.5;\n' + + 'int HalfDot = Half*1.5;\n' + + 'int QuarterDot = Quarter*1.5;\n' + + 'int EigthDot = Eigth*1.5;\n' + + 'int SixteenthDot = Sixteenth*1.5;\n' + + 'int ThirtysecondDot = Thirtysecond*1.5;\n' + + '\n' + + 'int Frequency(int note, int octave)\n' + + '{\n' + + ' const int table[] = {4186, 4434, 4698, 4978, 5274, 5587, 5915, 6271, 6644, 7040, 7458, 7902};\n' + + ' return table[note] >> (4-octave);\n' + + '}\n' + + '\n' + + 'void Play(int note, int octave, int length)\n' + + '{\n' + + ' if (note != None) \n' + + ' Sound(Frequency(note, octave));\n' + + '\n' + + ' //BIOS::DBG::Print("len=%d, ", length);\n' + + ' BIOS::SYS::DelayMs(length);\n' + + ' SoundOff();\n' + + ' BIOS::SYS::DelayMs(10);\n' + + '}\n' + + '\n' + + 'int main()\n' + '{\n' + - ` BIOS::DBG::Print("
");\n` + - ` BIOS::DBG::Print("
");\n` + - ` BIOS::DBG::Print("
On top
");\n` + - ' BIOS::DBG::Print("
");\n' + + ' // generated from http://x.valky.eu/klavirgen\n' + + ' Play(G, 2, Quarter);\n' + + ' Play(Ais, 2, EigthDot);\n' + + ' Play(G, 2, Sixteenth);\n' + + ' Play(None, 0, Sixteenth);\n' + + ' Play(G, 2, Sixteenth);\n' + + ' Play(C, 3, Eigth);\n' + + ' Play(G, 2, Eigth);\n' + + ' Play(F, 2, Eigth);\n' + + ' Play(G, 2, Quarter);\n' + + ' Play(D, 3, EigthDot);\n' + + ' Play(G, 2, Sixteenth);\n' + + ' Play(None, 0, Sixteenth);\n' + + ' Play(G, 2, Sixteenth);\n' + + ' Play(Dis, 3, Eigth);\n' + + ' Play(D, 3, Eigth);\n' + + ' Play(Ais, 2, Eigth);\n' + + ' Play(G, 2, Eigth);\n' + + ' Play(D, 3, Eigth);\n' + + ' Play(G, 3, Eigth);\n' + + ' Play(G, 2, Sixteenth);\n' + + ' Play(F, 2, Sixteenth);\n' + + ' Play(None, 0, Sixteenth);\n' + + ' Play(F, 2, Sixteenth);\n' + + ' Play(D, 2, Eigth);\n' + + ' Play(A, 2, Eigth);\n' + + ' Play(G, 2, Half);\n' + + '\n' + ' return 0;\n' + - '}\n', - 'return.cpp': '#include \n\nint main(void)\n{\n return 724;\n}\n' + '}\n' } diff --git a/system/apps_featured/117_gabuino/web/v1/index.html b/system/apps_featured/117_gabuino/web/v1/index.html index 2e8ada3d..1c5a155a 100644 --- a/system/apps_featured/117_gabuino/web/v1/index.html +++ b/system/apps_featured/117_gabuino/web/v1/index.html @@ -277,22 +277,186 @@

Testing

using namespace BIOS; -__attribute__((__section__(".entry"))) +typedef int fix16_t; +void drawline_aa(fix16_t fx1, fix16_t fy1, fix16_t fx2, fix16_t fy2, int color); + int main(void) { - BIOS::KEY::EKey key; - while ((key = KEY::GetKey()) != BIOS::KEY::EKey::Escape) + int x = 100, y = 100, c = 0; + KEY::EKey key; + while ((key = KEY::GetKey()) != KEY::EKey::Escape) { - EVERY(5000) - { - BIOS::DBG::Print("Ahoj!"); - } - SYS::DelayMs(50); + int x1 = rand() % LCD::Width; + int y1 = 16+(rand() % (LCD::Height-32)); + int c = rand() & 0xffff; + drawline_aa(x*256, y*256, x1*256, y1*256, c); + x = x1; + y = y1; + + if ((c++ % 1000) == 0) + DBG::Print("%d lines, ", c); } return 0; } - + + + +// https://github.com/PetteriAimonen/QuadPawn/blob/master/Runtime/drawing.c + +/* Antialiased line drawing */ + +// Alpha-blend two colors together. Alpha is 0 to 255. +// The ratios have been biased a bit to make the result look +// better on a cheap TFT. +int blend(int fg, int bg, int alpha) +{ + int fg_per_2 = (fg & 0xF7DE) >> 1; + int fg_per_4 = (fg & 0xE79C) >> 2; + int fg_per_8 = (fg & 0xC718) >> 3; + + int bg_per_2 = (bg & 0xF7DE) >> 1; + int bg_per_4 = (bg & 0xE79C) >> 2; + int bg_per_8 = (bg & 0xC718) >> 3; + + if (alpha > 224) + return fg; // 100% blend + else if (alpha > 192) + return (fg - fg_per_8 + bg_per_8); // 88% blend + else if (alpha > 128) + return (fg - fg_per_4 + bg_per_4); // 75% blend + else if (alpha > 64) + return (fg_per_2 + bg_per_2); // 50% blend + else if (alpha > 32) + return (fg_per_4 + bg - bg_per_4); // 25% blend + else + return bg; // 0% blend +} + + +// Draws antialiased lines +// Xiaolin Wu's algorithm, using x/256 fixed point values +void drawline_aa(fix16_t x1, fix16_t y1, fix16_t x2, fix16_t y2, int color) +{ + bool reverse_xy = false; + + auto swap = [](int *x, int *y) { + int temp = *x; + *x = *y; + *y = temp; + }; + + // plot the pixel at (x, y) with brightness c + auto plot = [&](int x, int y, int c) { + if (reverse_xy) + swap(&x, &y); + + uint16_t oldcolor = BIOS::LCD::GetPixel(x >> 8, y >> 8); + BIOS::LCD::PutPixel(x >> 8, y >> 8, blend(color, oldcolor, c)); + }; + + // Integer part of x + auto ipart = [](int x) -> int { + return x & (~0xFF); + }; + + auto round = [&](int x) -> int { + return ipart(x + 128); + }; + + // Fractional part of x + auto fpart = [](int x) -> int { + return x & 0xFF; + }; + + // Remaining fractional part of x + auto rfpart = [&](int x) -> int { + return 256 - fpart(x); + }; + + int dx = x2 - x1; + int dy = y2 - y1; + if (abs(dx) < abs(dy)) + { + swap(&x1, &y1); + swap(&x2, &y2); + swap(&dx, &dy); + reverse_xy = true; + } + + if (x2 < x1) + { + swap(&x1, &x2); + swap(&y1, &y2); + } + + int gradient = dy * 256 / dx; + + // handle first endpoint + int xend = round(x1); + int yend = y1 + gradient * (xend - x1) / 256; + int xgap = rfpart(x1 + 128); + int xpxl1 = xend; // this will be used in the main loop + int ypxl1 = ipart(yend); + plot(xpxl1, ypxl1, rfpart(yend) * xgap / 256); + plot(xpxl1, ypxl1 + 256, fpart(yend) * xgap / 256); + int intery = yend + gradient; // first y-intersection for the main loop + + // handle second endpoint + xend = round(x2); + yend = y2 + gradient * (xend - x2) / 256; + xgap = fpart(x2 + 128); + int xpxl2 = xend; // this will be used in the main loop + int ypxl2 = ipart(yend); + plot(xpxl2, ypxl2, rfpart(yend) * xgap / 256); + plot(xpxl2, ypxl2 + 256, fpart(yend) * xgap / 256); + + // main loop + for (int x = xpxl1 + 1; x <= xpxl2 - 1; x += 256) + { + plot(x, ipart(intery), rfpart(intery)); + plot(x, ipart(intery) + 256, fpart(intery)); + intery = intery + gradient; + } +} + +/* Non-antialiased line drawing */ + +void drawline(int x1, int y1, int x2, int y2, int color, int dots) +{ + // Algorithm from here: http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm#Simplification + int dx = abs(x2 - x1); + int dy = abs(y2 - y1); + + int sx = (x1 < x2) ? 1 : -1; + int sy = (y1 < y2) ? 1 : -1; + + int err = dx - dy; + int count = 0; + for(;; count++) + { + if (!dots || (count >> (dots - 1)) & 1) + { + //__Point_SCR(x1, y1); + //__LCD_SetPixl(color); + } + + if (x1 == x2 && y1 == y2) break; + + int e2 = 2 * err; + if (e2 > -dy) + { + err -= dy; + x1 += sx; + } + else if (e2 < dx) + { + err += dx; + y1 += sy; + } + } +} + \ No newline at end of file