Skip to content

Commit

Permalink
Gabuino flappy bird, melody player, init_array fix
Browse files Browse the repository at this point in the history
  • Loading branch information
gabonator committed Aug 5, 2021
1 parent 22e3530 commit 087d712
Show file tree
Hide file tree
Showing 11 changed files with 634 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int main(void)
y = y1;

if ((c++ % 1000) == 0)
DBG::Print("%d lines, ", c/1000);
DBG::Print("<b>%d</b> lines, ", c);
}

return 0;
Expand Down
48 changes: 48 additions & 0 deletions system/apps_featured/117_gabuino/examples/flappybird.cpp
Original file line number Diff line number Diff line change
@@ -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 <library.h>

int main(void)
{
BIOS::DBG::Print(R"(<canvas id="c" width="400" height="400" style="background:gray"></canvas>)");

BIOS::DBG::Print(R"(<script>
context = document.getElementById("c").getContext("2d");
bird = new Image();
bird.src = "https://raw.githubusercontent.com/danba340/tiny-flappy-bird/master/bird.png";
birdX = birdDY = score = bestScore = 0;
interval = birdSize = pipeWidth = topPipeBottomY = 24;
birdY = pipeGap = 200;
canvasSize = pipeX = 400;
c.onclick = () => (birdDY = 9);
setInterval(() => {
context.fillStyle = "skyblue";
context.fillRect(0,0,canvasSize,canvasSize); // Draw sky
birdY -= birdDY -= 0.5; // Gravity
context.drawImage(bird, birdX, birdY, birdSize * (524/374), birdSize); // Draw bird
context.fillStyle = "green";
pipeX -= 8; // Move pipe
pipeX < -pipeWidth && // Pipe off screen?
((pipeX = canvasSize), (topPipeBottomY = pipeGap * Math.random())); // Reset pipe and randomize gap.
context.fillRect(pipeX, 0, pipeWidth, topPipeBottomY); // Draw top pipe
context.fillRect(pipeX, topPipeBottomY + pipeGap, pipeWidth, canvasSize); // Draw bottom pipe
context.fillStyle = "black";
context.fillText(score++, 9, 25); // Increase and draw score
bestScore = bestScore < score ? score : bestScore; // New best score?
context.fillText('Best: '+bestScore, 9, 50); // Draw best score
(((birdY < topPipeBottomY || birdY > topPipeBottomY + pipeGap) && pipeX < birdSize * (524/374))// Bird hit pipe?
|| birdY > canvasSize) && // Bird falls off screen
((birdDY = 0), (birdY = 200), (pipeX = canvasSize), (score = 0)); // Bird died
}, interval)
</script>)");

BIOS::KEY::EKey key;
while ((key = BIOS::KEY::GetKey()) != BIOS::KEY::EKey::Escape)
{
if (key == BIOS::KEY::EKey::Enter)
BIOS::DBG::Print(R"(<script>birdDY=9</script>)");
}
return 0;
}
10 changes: 0 additions & 10 deletions system/apps_featured/117_gabuino/examples/map.cpp

This file was deleted.

98 changes: 98 additions & 0 deletions system/apps_featured/117_gabuino/examples/melody.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// plays Axel F on internal piezo speaker of LA104
#include <library.h>
#include <math.h>

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;
}
6 changes: 0 additions & 6 deletions system/apps_featured/117_gabuino/examples/return.cpp

This file was deleted.

2 changes: 1 addition & 1 deletion system/apps_featured/117_gabuino/source/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ namespace MEMORY

int Exec(uint32_t ptr)
{
_ASSERT(ptr == 0x20005001);
_ASSERT((ptr & 1) && (ptr >= 0x20005001));
userRetVal = 0x66667777;

typedef int(*TFunc)();
Expand Down
40 changes: 37 additions & 3 deletions system/apps_featured/117_gabuino/web/v1/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<len; i++)
Expand Down Expand Up @@ -67,9 +80,18 @@ function flash()
}

function run()
{
BIOS.exec(globalOffset|1)//.then( () => 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<globalInit.length; i++)
// BIOS.exec(globalInit[i]|1); // we should chain these!
}

prepare.then( () => BIOS.exec(globalOffset|1) )//.then( () => resolve() )
// we do not know if the program keeps running or has ended with return code
return Promise.resolve();
}

Expand All @@ -80,6 +102,7 @@ function stop()

var globalOffset;
var globalBlob;
var globalInit;
var globalResolve = [];
function processElf(elf)
{
Expand All @@ -96,13 +119,23 @@ function processElf(elf)
throw "too big blob";
// TODO: BSS!!!!
var appblob = new Uint8Array(end-begin);
globalBlob = appblob;
var init = [];
for (var i=0; i<elfinfo.sections.length; i++)
{
var section = elfinfo.sections[i];
if (section.name != ".bss")
section.data.copyTo(appblob, section.addr - begin);
else
clearBss(appblob, section.addr - begin, section.size);

if (section.name == ".init_array")
{
globalOffset = begin;
globalBlob = appblob;
for (var i=0; i<section.size; i+=4)
init.push(readDword(section.addr + i));
}
}
for (var i=0; i<elfinfo.relocations.length; i++)
{
Expand All @@ -115,6 +148,7 @@ function processElf(elf)
console.log(appblob);
globalOffset = begin;
globalBlob = appblob;
globalInit = init;
resolve(elfinfo)
// continueResolve(resolve);
});
Expand Down

0 comments on commit 087d712

Please sign in to comment.