Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: ivanjovanovic/0x10c
base: ae16df1fe2
...
head fork: ivanjovanovic/0x10c
compare: 0a2b24b283
Checking mergeability… Don't worry, you can still create the pull request.
  • 3 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
View
7 dcpu16/assembler/assembler.go
@@ -7,15 +7,16 @@ var byteCode []dcpu16.Word
/* var labels map[string]word*/
/* var labelPlaceholders map[string][]word*/
-func Assemble(program []string) []dcpu16.Word {
+
+func Assemble(program string) []dcpu16.Word {
// initialize vars
byteCode := make([]dcpu16.Word, dcpu16.MEMORY_SIZE)
/* labels := make(map[string]word)*/
/* labelPlaceholders := make(map[string][]word)*/
- for _, line := range program {
- line = line
+ for i, _ := range program {
+ byteCode[i] = dcpu16.Word(i)
}
return byteCode
View
82 dcpu16/assembler/assembler_test.go
@@ -1 +1,81 @@
- package assembler
+package assembler
+
+import "testing"
+import "os"
+import "bufio"
+import "io"
+import "io/ioutil"
+import "github.com/ivanjovanovic/0x10c/dcpu16"
+import "fmt"
+
+func readLines(filename string) []string {
+ file, _ := os.Open(filename)
+ reader := bufio.NewReader(file)
+ lines := []string{}
+
+ for {
+ line, _, _ := reader.ReadLine()
+ if _, err := reader.Peek(1); err == io.EOF {
+ break
+ }
+ lines = append(lines, string(line))
+ }
+ return lines
+}
+
+func readFile(filename string) string {
+ file, _ := os.Open(filename)
+ reader := bufio.NewReader(file)
+
+ contents, err := ioutil.ReadAll(reader)
+
+ if err != nil {
+ panic(err.Error())
+ }
+
+ return string(contents)
+}
+
+func assertSame(specified, assembled []string) bool {
+ for i, _ := range specified {
+ if specified[i] != assembled[i] {
+ return false
+ }
+ }
+ return true
+}
+
+func castMemoryContent(memory []dcpu16.Word) []string {
+ memString := []string{}
+
+ for i, _ := range memory {
+ memString = append(memString, fmt.Sprintf("%04x", int(memory[i])))
+ }
+ return memString
+}
+
+// Print nice table with specified and comparable memory
+func printMemoryComparison(spec, assembled []string) {
+ fmt.Print("Specified\tAssembled\t\n")
+ for i, val := range spec {
+ fmt.Printf(" %s\t\t %s\n", val, assembled[i])
+ }
+}
+
+// Tests for official specification given on DCPU16 spec page
+// http://0x10c.com/doc/dcpu-16.txt
+func TestAssemblerSpecification(t *testing.T) {
+ program := readFile("test/spec.dasm")
+ memorySpec := readLines("test/spec.mem")
+
+ memoryImage := Assemble(program)
+
+ comparableMemoryImage := castMemoryContent(memoryImage)[0:len(memorySpec)]
+
+ same := assertSame(memorySpec, comparableMemoryImage)
+
+ if !same {
+ printMemoryComparison(memorySpec, comparableMemoryImage)
+ t.Error("Returned memory different from specified")
+ }
+}

No commit comments for this range

Something went wrong with that request. Please try again.