Commits

Justin Poliey committed 3ca533c

First commit

Comments (0)

Files changed (4)

+package {
+	import flash.display.Sprite;
+	[SWF(width="320", height="240", backgroundColor="0", frameRate="30")]
+	public class Blip extends Sprite {
+		public function Blip() {
+			main = this;
+			initialize();
+		}
+	}
+}
+
+import flash.display.*;
+import flash.events.*;
+import com.beam.blip.*;
+
+var main:Sprite;
+var g:Graphics;
+var screen:Display;
+
+function initialize():void {
+	/* Initialize the screen */
+	g = main.graphics;
+	main.addEventListener(Event.ENTER_FRAME, update);
+}
+
+function update(event:Event):void {
+	g.clear();
+	g.beginFill(0xffffff);
+	for (var y:uint = 0; y < 48; y++) {
+		for (var x:uint = 0; x < 64; x++) {
+			g.drawRect(x * 5, y * 5, 5, 5);
+		}
+	}
+	g.endFill();
+}
+

com/beam/blip/Display.as

+package com.beam.blip {
+
+	public class Display {
+	
+		public var width:uint;
+		public var height:uint;
+		private var pixels:Array;
+	
+		function Display(_width:uint, _height:uint) {
+			width = width;
+			height = height;
+			pixels = new Array(width);
+			for (var x:uint = 0; x < width; x++) {
+				pixels[x] = new Array(height);
+				for (var y:uint = 0; y < height; y++) {
+					pixels[x][y] = false;
+				}
+			}
+		}
+	
+		public function getPixel(x:uint, y:uint):Boolean {
+			return pixels[x][y];
+		}
+	
+		public function setPixel(x:uint, y:uint, value:Boolean):void {
+			pixels[x][y] = value;
+		}
+	
+		public function clear():void {
+			for (var y:uint = 0; y < height; y++) {
+				for (var x:uint = 0; y < width; x++) {
+					pixels[x][y] = false;
+				}
+			}
+		}
+	
+	}
+	
+}

com/beam/blip/Emulator.as

+package com.beam.blip {
+
+	public class Emulator {
+	
+		public var screen:Display;
+		public var keys:Array;
+		public var loader:RomLoader;
+		private var memory:Vector.<int>;
+		private var regs:Vector.<int>;
+		private var stack:Vector.<int>;
+		private var counter:uint;
+		private var pointer:int;
+		private var delay:int;
+	
+		function Emulator() {
+			screen = new Display(64, 48);
+			loader = new RomLoader();
+			memory = new Vector.<int>(4096);
+			regs = new Vector.<int>(16);
+			stack = new Vector.<int>();
+			reset(true);
+		}
+		
+		public function reset(hard:Boolean = false):void {
+			screen.clear();
+			if (hard) {
+				for (var i:uint = 0; i < 4096; i++) {
+					memory[i] = 0;
+				}
+			}
+			counter = 0;
+			pointer = 0;
+			stack.splice(0);
+		}
+		
+		public function load(rom:String):void {
+			var rl:RomLoader = new RomLoader(rom);
+			
+		}
+
+		public function step():bool {
+			var word:uint = (memory[counter++] << 8) + memory[counter++];
+			switch ((word & 0xf000) >> 12) {
+				case 0x0:
+					if (word == 0x0000) {
+						/* 0x0000 end */
+						return false;
+					}
+					break;
+				case 0x1:
+					/* 0x1rxx loadl gr, xx */
+					regs[(word & 0x0f00) >> 8] = word & 0x00ff;
+					break;
+				case 0x2:
+					switch ((word & 0x0f00) >> 8) {
+						case 0x0:
+							/* 0x20r0 loadg gr */
+							regs[(word & 0x00f0) >> 4] = memory[pointer];
+							break;
+						case 0x1:
+							/* 0x21r0 storeg gr */
+							memory[pointer] = regs[(word & 0x00f0) >> 4];
+							break;
+					}
+					break;
+				case 0x3:
+					/* 0x3rxx random gr, xx */
+					regs[(word & 0x0f00) >> 8] = Math.floor(Math.random() * ((word & 0x00ff) + 1));
+					break;
+				case 0x4:
+					switch ((word & 0x0f00) >> 8) {
+						case 0x0:
+							/* 0x40rs copy gr, gs */
+							regs[(word & 0x00f0) >> 4] = regs[word & 0x000f];
+							break;
+						case 0x1:
+							/* 0x41rs add gr, gs */
+							regs[(word & 0x00f0) >> 4] += regs[word & 0x000f];
+							if (regs[(word & 0x00f0) >> 4] > 255) {
+								regs[(word & 0x00f0) >> 4] -= 255;
+								regs[0xf] = 1;
+							}
+							break;
+						case 0x2:
+							/* 0x42rs sub gr, gs */
+							regs[(word & 0x00f0) >> 4] -= regs[word & 0x000f];
+							if (regs[(word & 0x00f0) >> 4] < 0) {
+								regs[(word & 0x00f0) >> 4) += 255;
+								regs[0xf] = 1;
+							}
+							break;
+						case 0x3:
+							/* 0x43rs and gr, gs */
+							regs[(word & 0x00f0) >> 4) &= regs[word & 0x000f];
+							break;
+						case 0x4:
+							/* 0x44rs or gr, gs */
+							regs[(word & 0x00f0) >> 4) |= regs[word & 0x000f];
+							break;
+						case 0x5:
+							/* 0x45rs xor gr, gs */
+							regs[(word & 0x00f0) >> 4) ^= regs[word & 0x000f];
+							break;
+						case 0x6:
+							/* 0x46rs skeq gr, gs */
+							if (regs[(word & 0x00f0) >> 4] == regs[word & 0x000f]) {
+								counter += 2;
+							}
+							break;
+						case 0x7:
+							/* 0x47rs skne gr, gs */
+							if (regs[(word & 0x00f0) >> 4] != regs[word & 0x000f]) {
+								counter += 2;
+							}
+							break;
+						case 0x8:
+							switch (regs[word & 0x000f]) {
+								case 0x0:
+									/* 0x48r0 add p, gr */
+									pointer += regs[(word & 0x00f0) >> 4];
+									if (pointer >= 4096) {
+										pointer -= 4096;
+										regs[0xf] = 1;
+									}
+									break;
+								case 0x1:
+									/* 0x48r1 sub p, gr */
+									pointer -= regs[(word & 0x00f0) >> 4];
+									if (pointer <= 4096) {
+										pointer += 4096;
+										regs[0xf] = 1;
+									}
+									break;
+							}
+							break;
+						case 0x9:
+							switch (regs[word & 0x000f]) {
+								case 0x0:
+									/* 0x49r0 push gr */
+									stack.push(regs[(word & 0x00f0) >> 4]);
+									break;
+								case 0x1:
+									/* 0x49r1 pop gr */
+									regs[(word & 0x00f0) >> 4] = stack.pop();
+									break;
+							}
+							break;
+						case 0xa:
+							switch (regs[word & 0x000f]) {
+								case 0x0:
+									/* 0x4a00 push p */
+									stack.push(pointer & 0x00ff);
+									stack.push((pointer & 0xff00) >> 8);
+									break;
+								case 0x1:
+									/* 0x4a01 pop p */
+									pointer = ((stack.pop() << 8) + stack.pop()) & 0x0fff;
+									break;
+							}
+							break;
+						case 0xb:
+							/* 0x4b00 retsb */
+							counter = ((stack.pop() << 8) + stack.pop()) & 0x0fff;
+							break;
+						case 0xc:
+							/* 0x4cxx delay xx */
+							delay = word & 0x00ff;
+							break;
+						case 0xd:
+							/* 0x4dr0 delay gr */
+							delay = regs[(word & 0x00f0) >> 4];
+							break;
+					}
+					break;
+				}
+				break;
+			case 0x5:
+				/* 0x5rxx add gr, xx */
+				regs[(word & 0x0f00) >> 8] += word & 0x00ff;
+				if (regs[(word & 0x0f00) >> 8] >= 256) {
+					regs[(word & 0x0f00) >> 8] -= 256;
+					regs[0xf] = 1;
+				}
+				break;
+			case 0x6:
+				/* 0x6rxx sub gr, xx */
+				regs[(word & 0x0f00) >> 8] -= word & 0x00ff;
+				if (regs[(word & 0x0f00) >> 8] < 0) {
+					regs[(word & 0x0f00) >> 8] += 256;
+					regs[0xf] = 1;
+				}
+				break;
+			case 0x7:
+				/* 0x7rxx skeq gr, xx */
+				if (regs[(word & 0x0f00) >> 8] == (word & 0x00ff)) {
+					counter += 2;
+				}
+				break;
+			case 0x8:
+				/* 0x8rxx skne gr, xx */
+				if (regs[(word & 0x0f00) >> 8] != (word & 0x00ff)) {
+					counter += 2;
+				}
+				break;
+			case 0x9:
+				/* 0x9xxx jump xxx */
+				counter = word & 0x0fff;
+				break;
+			case 0xa:
+				/* 0xaxxx jumpsb xxx */
+				stack.push(counter & 0x00ff);
+				stack.push((counter & 0xff00) >> 8);
+				counter = word & 0x0fff;
+				break;
+			case 0xb:
+				/* 0xbxxx point xxx */
+				pointer = word & 0x0fff;
+				break;
+			case 0xc:
+				/* 0xcrsx sprite gr, gs, x */
+				regs[0xf] = 0;
+				var x:int, y:int;
+				for (var _y:int = 0; _y < (word & 0x000f); _y++) {
+					for (var _x:int = 0; _x < 8; _x++) {
+						x = (regs[(word & 0x0f00) >> 8] + _x) % screen.width;
+						y = (regs[(word & 0x00f0) >> 4] + _y) % screen.height;
+						var pixel:Boolean = memory[(pointer + _y) % 4096] & (1 << (8 - _x)));
+						if (pixel) {
+							regs[0xf] = 1;
+						}
+						screen.setPixel(x, y, pixel);
+					}
+				}
+				break;
+			case 0xd:
+				/* 0xdr00 score gr */
+				break;
+			case 0xe:
+				/* 0xex00 digit x */
+				break;
+			case 0xf:
+				/* 0xf000 clear */
+				screen.clear();
+				break;
+		}
+		
+	}
+	
+}

com/beam/blip/RomLoader.as

+package com.beam.blip {
+
+	public class RomLoader {
+	
+		private var loaded = false;
+		private var bytestring:String;
+		
+		public function RomLoader(rom:String = null) {
+			if (rom) {
+				load(rom);
+			}
+		}
+		
+		public function load(rom:String):void {
+			rom = rom.replace(/[^a-f0-9]/gi, "");
+			if (!(rom.length % 2)) {
+				rom = rom.concat("0");
+			}
+			bytestring = rom;
+			loaded = true;
+		}
+		
+		public function toBinary():Vector.<int> {
+			if (loaded) {
+				var blob = Vector.<int>();
+				for (var i = 0; (i <= bytestring.length / 2); i ++) {
+					_byte = parseInt("0x" + bytestring.substr(i * 2, 2));
+					blob.push(_byte);
+				}
+				return blob;
+			}
+			else {
+				return null;
+			}
+		}
+		
+	}
+
+}