Rdcpu16 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/dcpu16/assembler.rb +33 -0
- data/lib/dcpu16/memory.rb +13 -4
- data/lib/dcpu16/version.rb +1 -1
- data/lib/dcpu16/version.rb~ +1 -1
- data/lib/dcpu16.rb +1 -0
- data/spec/fixtures/example.asm +29 -0
- data/spec/unit/dcpu16_assembler_spec.rb +10 -0
- data/spec/unit/dcpu16_memory_spec.rb +10 -0
- data/spec/unit/support/memory_observer.rb +14 -0
- metadata +10 -3
@@ -0,0 +1,33 @@
|
|
1
|
+
# WIP
|
2
|
+
module DCPU16
|
3
|
+
class Assembler
|
4
|
+
attr_reader :input
|
5
|
+
def initialize(text)
|
6
|
+
@input = text
|
7
|
+
end
|
8
|
+
|
9
|
+
def dump
|
10
|
+
raise "TODO #{caller.first}"
|
11
|
+
lines = []
|
12
|
+
@input.each_line do |line|
|
13
|
+
empty = (line =~ /^\s*$/)
|
14
|
+
comment = (line =~ /^\s*;+.*$/)
|
15
|
+
|
16
|
+
next if empty || comment
|
17
|
+
|
18
|
+
line.gsub!(/^\s*([^;]*).*$/) { $1 } # Strip some spaces
|
19
|
+
|
20
|
+
regex = /^(:\w*)?\s*(\w*)\s*([^,\s]*),?\s?([^\s]*).*$/
|
21
|
+
match = line.match(regex)
|
22
|
+
|
23
|
+
line = { :label => match[1],
|
24
|
+
:op => match[2],
|
25
|
+
:a => match[3],
|
26
|
+
:b => match[4] }
|
27
|
+
puts line
|
28
|
+
lines << line
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
data/lib/dcpu16/memory.rb
CHANGED
@@ -1,12 +1,15 @@
|
|
1
1
|
require 'dcpu16/word'
|
2
|
+
require "observer"
|
2
3
|
|
3
4
|
module DCPU16
|
4
|
-
class Memory < Array
|
5
|
+
class Memory# < Array
|
6
|
+
include Observable
|
7
|
+
|
5
8
|
SIZE = 0x10000
|
6
9
|
DEFAULT_VALUE = 0x0
|
7
10
|
|
8
11
|
def initialize(default = [])
|
9
|
-
|
12
|
+
@memory = Array.new(SIZE, DEFAULT_VALUE)
|
10
13
|
default.each_with_index { |word, offset| write(offset, word) }
|
11
14
|
end
|
12
15
|
|
@@ -14,19 +17,25 @@ module DCPU16
|
|
14
17
|
# HACK: so we can just pass a Fixnum or a Register
|
15
18
|
offset = offset.value if offset.respond_to? :value
|
16
19
|
|
17
|
-
DCPU16::Word.new(
|
20
|
+
DCPU16::Word.new(@memory[offset], self, offset)
|
18
21
|
end
|
19
22
|
|
20
23
|
def write(offset, value)
|
21
24
|
# HACK: so we can just pass a Fixnum or a Register
|
22
25
|
offset = offset.value if offset.respond_to? :value
|
23
26
|
|
24
|
-
|
27
|
+
@memory[offset] = value
|
28
|
+
changed
|
29
|
+
notify_observers(offset, value)
|
25
30
|
end
|
26
31
|
|
27
32
|
def reset
|
28
33
|
end
|
29
34
|
|
35
|
+
def length
|
36
|
+
@memory.length
|
37
|
+
end
|
38
|
+
|
30
39
|
private
|
31
40
|
def [](key)
|
32
41
|
super(key)
|
data/lib/dcpu16/version.rb
CHANGED
data/lib/dcpu16/version.rb~
CHANGED
data/lib/dcpu16.rb
CHANGED
@@ -0,0 +1,29 @@
|
|
1
|
+
; Try some basic stuff
|
2
|
+
SET A, 0x30 ; 7c01 0030
|
3
|
+
SET [0x1000], 0x20 ; 7de1 1000 0020
|
4
|
+
SUB A, [0x1000] ; 7803 1000
|
5
|
+
IFN A, 0x10 ; c00d
|
6
|
+
SET PC, crash ; 7dc1 001a [*]
|
7
|
+
|
8
|
+
; Do a loopy thing
|
9
|
+
SET I, 10 ; a861
|
10
|
+
SET A, 0x2000 ; 7c01 2000
|
11
|
+
:loop SET [0x2000+I], [A] ; 2161 2000
|
12
|
+
SUB I, 1 ; 8463
|
13
|
+
IFN I, 0 ; 806d
|
14
|
+
SET PC, loop ; 7dc1 000d [*]
|
15
|
+
|
16
|
+
; Call a subroutine
|
17
|
+
SET X, 0x4 ; 9031
|
18
|
+
JSR testsub ; 7c10 0018 [*]
|
19
|
+
SET PC, crash ; 7dc1 001a [*]
|
20
|
+
|
21
|
+
:testsub SHL X, 4 ; 9037
|
22
|
+
SET PC, POP ; 61c1
|
23
|
+
|
24
|
+
; Hang forever. X should now be 0x40 if everything went right.
|
25
|
+
:crash SET PC, crash ; 7dc1 001a [*]
|
26
|
+
|
27
|
+
; [*]: Note that these can be one word shorter and one cycle faster by using the short form (0x00-0x1f) of literals,
|
28
|
+
; but my assembler doesn't support short form labels yet.
|
29
|
+
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'spec_helper.rb'
|
2
|
+
|
3
|
+
describe DCPU16::Assembler do
|
4
|
+
let(:asm) { File.open(File.join('spec', 'fixtures', 'example.asm')).read }
|
5
|
+
subject { DCPU16::Assembler.new(asm) }
|
6
|
+
|
7
|
+
its(:input) { should == asm }
|
8
|
+
# its(:dump) { should be_a_kind_of(Array) }
|
9
|
+
end
|
10
|
+
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'spec_helper.rb'
|
2
|
+
require 'unit/support/memory_observer'
|
2
3
|
|
3
4
|
describe DCPU16::Memory do
|
4
5
|
its(:length) { should == 0x10000 }
|
@@ -12,5 +13,14 @@ describe DCPU16::Memory do
|
|
12
13
|
subject.write(0x1000, 42)
|
13
14
|
subject.read(0x1000).value.should == 42
|
14
15
|
end
|
16
|
+
|
17
|
+
describe "Observable" do
|
18
|
+
let(:observer) { DCPU16::MemoryObserver.new }
|
19
|
+
specify do
|
20
|
+
subject.add_observer(observer)
|
21
|
+
expect { subject.write(0x9000, 42) }.to change{observer.offset}.to(0x9000)
|
22
|
+
expect { subject.write(0x9000, 41) }.to change{observer.value}.to(41)
|
23
|
+
end
|
24
|
+
end
|
15
25
|
end
|
16
26
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: Rdcpu16
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-04-09 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &76639670 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '2.6'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *76639670
|
25
25
|
description: This is a simple Ruby port of the fictive 16-bit-cpu DCPU16.
|
26
26
|
email:
|
27
27
|
- deradon87@gmail.com
|
@@ -30,6 +30,7 @@ extensions: []
|
|
30
30
|
extra_rdoc_files: []
|
31
31
|
files:
|
32
32
|
- lib/dcpu16/cpu.rb
|
33
|
+
- lib/dcpu16/assembler.rb
|
33
34
|
- lib/dcpu16/operand.rb
|
34
35
|
- lib/dcpu16/memory.rb
|
35
36
|
- lib/dcpu16/word.rb
|
@@ -44,9 +45,12 @@ files:
|
|
44
45
|
- MIT-LICENSE
|
45
46
|
- Rakefile
|
46
47
|
- README.rdoc
|
48
|
+
- spec/fixtures/example.asm
|
47
49
|
- spec/integration/example1_spec.rb
|
50
|
+
- spec/unit/dcpu16_assembler_spec.rb
|
48
51
|
- spec/unit/dcpu16_word_spec.rb
|
49
52
|
- spec/unit/support/sample_observer.rb
|
53
|
+
- spec/unit/support/memory_observer.rb
|
50
54
|
- spec/unit/dcpu16_register_spec.rb
|
51
55
|
- spec/unit/dcpu16_cpu_spec.rb
|
52
56
|
- spec/unit/dcpu16_memory_spec.rb
|
@@ -79,9 +83,12 @@ signing_key:
|
|
79
83
|
specification_version: 3
|
80
84
|
summary: Ruby port of DCPU16
|
81
85
|
test_files:
|
86
|
+
- spec/fixtures/example.asm
|
82
87
|
- spec/integration/example1_spec.rb
|
88
|
+
- spec/unit/dcpu16_assembler_spec.rb
|
83
89
|
- spec/unit/dcpu16_word_spec.rb
|
84
90
|
- spec/unit/support/sample_observer.rb
|
91
|
+
- spec/unit/support/memory_observer.rb
|
85
92
|
- spec/unit/dcpu16_register_spec.rb
|
86
93
|
- spec/unit/dcpu16_cpu_spec.rb
|
87
94
|
- spec/unit/dcpu16_memory_spec.rb
|