From 2b9e97b8635c20c5a2b87789b1014489863d1994 Mon Sep 17 00:00:00 2001 From: Alexis211 Date: Fri, 13 Nov 2009 18:05:27 +0100 Subject: Added a game of life simulator demo --- Source/Applications/Demos/GOL.cpp | 95 +++++++++++++++++++++++++ Source/Applications/Demos/Makefile | 35 +++++++++ Source/Applications/Demos/asmdemo.asm | 30 ++++++++ Source/Applications/Demos/cxxdemo.cpp | 20 ++++++ Source/Applications/Demos/lib-melonasm.asm | 24 +++++++ Source/Applications/SampleApps/Makefile | 35 --------- Source/Applications/SampleApps/asmdemo.asm | 30 -------- Source/Applications/SampleApps/cxxdemo.cpp | 20 ------ Source/Applications/SampleApps/lib-melonasm.asm | 24 ------- 9 files changed, 204 insertions(+), 109 deletions(-) create mode 100644 Source/Applications/Demos/GOL.cpp create mode 100644 Source/Applications/Demos/Makefile create mode 100644 Source/Applications/Demos/asmdemo.asm create mode 100644 Source/Applications/Demos/cxxdemo.cpp create mode 100644 Source/Applications/Demos/lib-melonasm.asm delete mode 100644 Source/Applications/SampleApps/Makefile delete mode 100644 Source/Applications/SampleApps/asmdemo.asm delete mode 100644 Source/Applications/SampleApps/cxxdemo.cpp delete mode 100644 Source/Applications/SampleApps/lib-melonasm.asm (limited to 'Source/Applications') diff --git a/Source/Applications/Demos/GOL.cpp b/Source/Applications/Demos/GOL.cpp new file mode 100644 index 0000000..6dd1cdf --- /dev/null +++ b/Source/Applications/Demos/GOL.cpp @@ -0,0 +1,95 @@ +#include +#include +#include +#include + +int main(Vector args) { + if (!outvt.isBoxed()) { + outvt << "Error : cannot display GOL on a non-boxed terminal.\n"; + return 1; + } + + int h = outvt.height() - 1, w = outvt.width(); + + bool *cells = new bool[w * h]; + bool *newcells = new bool[w * h]; + + u32int delay = 100; + + for (u32int x = 0; x < w; x++) { + for (u32int y = 0; y < h; y++) { + cells[x * h + y] = false; + } + } + + while (1) { + //Display cells + outvt.moveCursor(0, 0); + for (u32int y = 0; y < h; y++) { + for (u32int x = 0; x < w; x++) { + if (cells[x * h + y]) { + outvt.setColor(0, 2); + } else { + outvt.setColor(7, 0); + } + outvt << " "; + } + } + outvt << "Press Ctrl+h for help"; + + //Compute next generation + for (u32int y = 0; y < h; y++) { + for (u32int x = 0; x < w; x++) { + u8int n = 0; + for (u32int yy = y - 1; yy <= y + 1; yy++) { + for (u32int xx = x - 1; xx <= x + 2; xx++) { + if (xx < w and yy < h and cells[xx * h + yy]) n++; + } + } + if (cells[x * h + y]) n--; + if ((cells[x * h + y] and n == 2) or n == 3) { + newcells[x * h + y] = true; + } else { + newcells[x * h + y] = false; + } + } + } + for (u32int x = 0; x < w; x++) { + for (u32int y = 0; y < h; y++) { + cells[x * h + y] = newcells[x * h + y]; + } + } + + keypress_t kp = invt.getKeypress(false, false); + if (kp.hascmd && (kp.modifiers & STATUS_CTRL)) { + if (kp.character == WChar("c")) { + break; + } else if (kp.character == WChar("r")) { + for (u32int i = 0; i < 20; i++) { + u64int x = Rand::rand() * w / Rand::max(); + u64int y = Rand::rand() * h / Rand::max(); + cells[x * h + y] = true; + } + } else if (kp.character == WChar("R")) { + for (u32int i = 0; i < h; i++) { + cells[i] = true; + cells[(2 * i) % (h - i)] = true; + cells[(w * i) % (h * w - i)] = true; + } + } else if (kp.character == WChar("h")) { + outvt << "** Melon's demo Game Of Life Simulator help :\n"; + outvt << " - ctrl+c : quit\n"; + outvt << " - ctrl+h : show this\n"; + outvt << " - ctrl+r : add some random cells\n"; + outvt << " - ctrl+R : add more cells, but not random\n"; + outvt << "Press any key to return to simultaor..."; + invt.getKeypress(); + } + } + + Thread::get().sleep(100); + } + + delete cells; + delete newcells; +} diff --git a/Source/Applications/Demos/Makefile b/Source/Applications/Demos/Makefile new file mode 100644 index 0000000..e66acce --- /dev/null +++ b/Source/Applications/Demos/Makefile @@ -0,0 +1,35 @@ +.PHONY: clean, mrproper + +ASM = nasm +ASMFLAGS = -f bin + +CXX = g++ +CXXFLAGS = -nostartfiles -nostdlib -ffreestanding -fno-exceptions -fno-rtti -I ../../Library/Common -I ../../Library/Interface -I ../../Library/Userland -D THIS_IS_MELON_USERLAND + +LD = ld +LDFLAGS = -T ../../Library/Link.ld -L ../../Library + +Applications = asmdemo cxxdemo GOL + +all: $(Applications) + echo "* Done with applications : $(Applications)" + +rebuild: mrproper all + +%: %.cpp + echo "* Compiling $<..." + $(CXX) $(CXXFLAGS) -c $< -o $@.o + echo "* Linking $@.o..." + $(LD) $(LDFLAGS) $@.o -o $@ + +%: %.asm + echo "* Compiling $<..." + $(ASM) $(ASMFLAGS) -o $@ $< + +clean: + echo "* Removing object files..." + rm -rf *.o + +mrproper: clean + echo "* Removing applications..." + rm -rf $(Applications) diff --git a/Source/Applications/Demos/asmdemo.asm b/Source/Applications/Demos/asmdemo.asm new file mode 100644 index 0000000..d57cc9b --- /dev/null +++ b/Source/Applications/Demos/asmdemo.asm @@ -0,0 +1,30 @@ +%include "lib-melonasm.asm" + +start: ; label used for calculating app size + mov ecx, [data] + mov ebx, ecx + mov eax, SC_WHEX + int 64 + mov eax, SC_PUTCH + mov ebx, 10 + int 64 +lblloop: + inc ecx + mov eax, SC_PUTCH ;temporarily defined for writing one char to screen + mov ebx, ecx + int 64 + mov eax, SC_SLEEP ;temporary syscall for sleeping + mov ebx, 30 ;20ms + int 64 + cmp ecx, 127 + jnz lblloop + mov eax, 0 + mov eax, SC_PUTCH + mov ebx, 10 ;newline + int 64 + int 66 ;finish task + +data: +dd 0x00000020 + +end: ; label used for calculating app size diff --git a/Source/Applications/Demos/cxxdemo.cpp b/Source/Applications/Demos/cxxdemo.cpp new file mode 100644 index 0000000..3d452e7 --- /dev/null +++ b/Source/Applications/Demos/cxxdemo.cpp @@ -0,0 +1,20 @@ +#include +#include +#include +#include +#include + +int main(const Vector& args) { + outvt << "Enter some text plz : "; + String s = invt.readLine(); + outvt << s; + Thread t = Thread::get(); + for (char c = ' '; c <= 'z'; c++) { + t.sleep((u32int)c / 4); + outvt.put(c); + } + outvt << "\n"; + outvt << "Salut les gens ! c'est le progrès !!!\nLe boeuf mort est juste là : "; + outvt << 0xDEADBEEF; + outvt << "\n"; +} diff --git a/Source/Applications/Demos/lib-melonasm.asm b/Source/Applications/Demos/lib-melonasm.asm new file mode 100644 index 0000000..0845770 --- /dev/null +++ b/Source/Applications/Demos/lib-melonasm.asm @@ -0,0 +1,24 @@ +[bits 32] + +%ifidn __OUTPUT_FORMAT__, bin +; create a MelonBinary output + +%define MEM_ORIGIN 0x10000000 + +dd 0xFEEDBEEF ; magic number ^^ +dd end - start +dd MEM_ORIGIN + +; the ($-$$) permits not taking into account the header above +[org MEM_ORIGIN - ($-$$)] + +%elifidn __OUTPUT_FORMAT__, elf +; create an elf object + +[global start] + +%endif + +%define SC_PUTCH 0xFFFFFF01 +%define SC_SLEEP 0xFFFFFF02 +%define SC_WHEX 0xFFFFFF03 diff --git a/Source/Applications/SampleApps/Makefile b/Source/Applications/SampleApps/Makefile deleted file mode 100644 index 05f79b1..0000000 --- a/Source/Applications/SampleApps/Makefile +++ /dev/null @@ -1,35 +0,0 @@ -.PHONY: clean, mrproper - -ASM = nasm -ASMFLAGS = -f bin - -CXX = g++ -CXXFLAGS = -nostartfiles -nostdlib -ffreestanding -fno-exceptions -fno-rtti -I ../../Library/Common -I ../../Library/Interface -I ../../Library/Userland -D THIS_IS_MELON_USERLAND - -LD = ld -LDFLAGS = -T ../../Library/Link.ld -L ../../Library - -Applications = asmdemo cxxdemo - -all: $(Applications) - echo "* Done with applications : $(Applications)" - -rebuild: mrproper all - -%: %.cpp - echo "* Compiling $<..." - $(CXX) $(CXXFLAGS) -c $< -o $@.o - echo "* Linking $@.o..." - $(LD) $(LDFLAGS) $@.o -o $@ - -%: %.asm - echo "* Compiling $<..." - $(ASM) $(ASMFLAGS) -o $@ $< - -clean: - echo "* Removing object files..." - rm -rf *.o - -mrproper: clean - echo "* Removing applications..." - rm -rf $(Applications) diff --git a/Source/Applications/SampleApps/asmdemo.asm b/Source/Applications/SampleApps/asmdemo.asm deleted file mode 100644 index d57cc9b..0000000 --- a/Source/Applications/SampleApps/asmdemo.asm +++ /dev/null @@ -1,30 +0,0 @@ -%include "lib-melonasm.asm" - -start: ; label used for calculating app size - mov ecx, [data] - mov ebx, ecx - mov eax, SC_WHEX - int 64 - mov eax, SC_PUTCH - mov ebx, 10 - int 64 -lblloop: - inc ecx - mov eax, SC_PUTCH ;temporarily defined for writing one char to screen - mov ebx, ecx - int 64 - mov eax, SC_SLEEP ;temporary syscall for sleeping - mov ebx, 30 ;20ms - int 64 - cmp ecx, 127 - jnz lblloop - mov eax, 0 - mov eax, SC_PUTCH - mov ebx, 10 ;newline - int 64 - int 66 ;finish task - -data: -dd 0x00000020 - -end: ; label used for calculating app size diff --git a/Source/Applications/SampleApps/cxxdemo.cpp b/Source/Applications/SampleApps/cxxdemo.cpp deleted file mode 100644 index 3d452e7..0000000 --- a/Source/Applications/SampleApps/cxxdemo.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include -#include -#include -#include - -int main(const Vector& args) { - outvt << "Enter some text plz : "; - String s = invt.readLine(); - outvt << s; - Thread t = Thread::get(); - for (char c = ' '; c <= 'z'; c++) { - t.sleep((u32int)c / 4); - outvt.put(c); - } - outvt << "\n"; - outvt << "Salut les gens ! c'est le progrès !!!\nLe boeuf mort est juste là : "; - outvt << 0xDEADBEEF; - outvt << "\n"; -} diff --git a/Source/Applications/SampleApps/lib-melonasm.asm b/Source/Applications/SampleApps/lib-melonasm.asm deleted file mode 100644 index 0845770..0000000 --- a/Source/Applications/SampleApps/lib-melonasm.asm +++ /dev/null @@ -1,24 +0,0 @@ -[bits 32] - -%ifidn __OUTPUT_FORMAT__, bin -; create a MelonBinary output - -%define MEM_ORIGIN 0x10000000 - -dd 0xFEEDBEEF ; magic number ^^ -dd end - start -dd MEM_ORIGIN - -; the ($-$$) permits not taking into account the header above -[org MEM_ORIGIN - ($-$$)] - -%elifidn __OUTPUT_FORMAT__, elf -; create an elf object - -[global start] - -%endif - -%define SC_PUTCH 0xFFFFFF01 -%define SC_SLEEP 0xFFFFFF02 -%define SC_WHEX 0xFFFFFF03 -- cgit v1.2.3