rips 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/example.rips +10 -3
- data/lib/rips.rb +15 -4
- data/lib/rips/assembler.rb +45 -15
- data/lib/rips/formats.rb +4 -1
- data/lib/rips/formats/aformat.rb +18 -0
- data/lib/rips/formats/bformat.rb +22 -0
- data/lib/rips/formats/dformat.rb +22 -0
- data/lib/rips/formats/format.rb +5 -0
- data/lib/rips/instructions.rb +3 -1
- data/lib/rips/instructions/add.rb +19 -0
- data/lib/rips/instructions/set.rb +1 -1
- data/lib/rips/instructions/sub.rb +19 -0
- data/lib/rips/utils.rb +5 -2
- data/lib/rips/variables.rb +4 -2
- data/lib/rips/variables/address.rb +35 -0
- data/lib/rips/variables/inmediate.rb +6 -6
- data/lib/rips/variables/port.rb +35 -0
- data/lib/rips/variables/register.rb +11 -21
- data/lib/rips/variables/variable.rb +13 -3
- data/lib/rips/version.rb +1 -1
- metadata +9 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cf8c8bebaebfc51d045b029332f2cd2e061a88bf
|
4
|
+
data.tar.gz: 308c2b211e7feb112d7bbe80e3532771004efbf1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: efa9aa5de99297bee011ab544cf5d19d7759750141f022fa14c73a3d89b799fe77cba869fc1fa1e8302a0d924bb2ba8bbad267226043aca648940e28e622b6e5
|
7
|
+
data.tar.gz: ebb7b52fae71dfa37e44ae6ba8bccca459343567fbda81d5354c5eec09a653a07019afdb8946611f7faa4af933b70b53a1bc9a5c9e48abc513753123784dd349
|
data/.gitignore
CHANGED
data/example.rips
CHANGED
data/lib/rips.rb
CHANGED
@@ -4,16 +4,27 @@ require "rips/error"
|
|
4
4
|
|
5
5
|
module Rips
|
6
6
|
|
7
|
+
debug=false
|
8
|
+
|
9
|
+
# Check arguments
|
10
|
+
if ARGV.include?("-d")
|
11
|
+
debug = true
|
12
|
+
ARGV.delete("-d")
|
13
|
+
elsif ARGV.include?("--debug")
|
14
|
+
debug = true
|
15
|
+
ARGV.delete("--debug")
|
16
|
+
end
|
17
|
+
|
18
|
+
Error::message(1) if ARGV.empty?
|
19
|
+
|
7
20
|
# Check for a valid file
|
8
|
-
if
|
9
|
-
Error::message(1)
|
10
|
-
elsif !File.exist? ARGV[0]
|
21
|
+
if !File.exist? ARGV[0]
|
11
22
|
Error::message(2)
|
12
23
|
elsif !File.readable? ARGV[0]
|
13
24
|
Error::message(3)
|
14
25
|
end
|
15
26
|
|
16
|
-
rips = Assembler.new
|
27
|
+
rips = Assembler.new(debug)
|
17
28
|
|
18
29
|
File.open(ARGV[0], "r") do |f|
|
19
30
|
f.each_line do |line|
|
data/lib/rips/assembler.rb
CHANGED
@@ -3,15 +3,17 @@ require "rips/instructions"
|
|
3
3
|
module Rips
|
4
4
|
class Assembler
|
5
5
|
|
6
|
+
# @debug: switch to show trace in console
|
6
7
|
# @input: array with assembly instructions
|
7
8
|
# @output: array with coded instructions
|
8
9
|
# @cmd: line split on tokens (name + arguments)
|
9
10
|
# @instruction: instruction instance
|
10
11
|
# @line: number of file's line
|
11
|
-
def initialize
|
12
|
+
def initialize (debug)
|
13
|
+
@debug = debug
|
12
14
|
@input = []
|
13
15
|
@output = []
|
14
|
-
@cmd
|
16
|
+
@cmd = {}
|
15
17
|
@instruction
|
16
18
|
@line = 1
|
17
19
|
end
|
@@ -26,18 +28,26 @@ module Rips
|
|
26
28
|
|
27
29
|
@input.each do |line|
|
28
30
|
|
29
|
-
|
30
|
-
|
31
|
+
# If line is empty -> next line
|
32
|
+
if !line.empty?
|
31
33
|
|
32
|
-
|
33
|
-
|
34
|
-
argument_size
|
35
|
-
argument_syntax
|
34
|
+
parse_input(line)
|
35
|
+
@instruction = nil
|
36
36
|
|
37
|
-
|
37
|
+
# If it's a comment -> show but not work with it
|
38
|
+
if line[0] != "#"
|
38
39
|
|
39
|
-
|
40
|
-
|
40
|
+
exists_instruction
|
41
|
+
@instruction = get_instruction
|
42
|
+
|
43
|
+
argument_size
|
44
|
+
argument_syntax
|
45
|
+
|
46
|
+
@instruction.set_arguments(@cmd[:arguments])
|
47
|
+
@output << @instruction.code
|
48
|
+
end
|
49
|
+
show if @debug
|
50
|
+
end
|
41
51
|
@line += 1
|
42
52
|
end
|
43
53
|
|
@@ -46,8 +56,17 @@ module Rips
|
|
46
56
|
|
47
57
|
# Codification log of instruction
|
48
58
|
def show
|
49
|
-
|
50
|
-
|
59
|
+
|
60
|
+
# If line was a comment -> @instruction should be nil
|
61
|
+
if @instruction.nil?
|
62
|
+
codification = ""
|
63
|
+
else
|
64
|
+
codification = @instruction.code.scan(/.{4}|.+/).join("_")
|
65
|
+
end
|
66
|
+
|
67
|
+
puts "@#{@line}:" \
|
68
|
+
"\t#{codification}" \
|
69
|
+
"\t#{@cmd[:comments]}"
|
51
70
|
end
|
52
71
|
|
53
72
|
# Generate output in "progfile.dat"
|
@@ -61,8 +80,19 @@ module Rips
|
|
61
80
|
|
62
81
|
# Split on tokens
|
63
82
|
def parse_input (line)
|
64
|
-
|
65
|
-
|
83
|
+
|
84
|
+
if line[0] == "#"
|
85
|
+
@cmd[:comments] = line
|
86
|
+
else
|
87
|
+
@cmd[:name] = line.split("#").first.split(" ").first.downcase
|
88
|
+
@cmd[:arguments] = line.split("#").first.split(@cmd[:name])
|
89
|
+
if !@cmd[:arguments].empty?
|
90
|
+
@cmd[:arguments] = @cmd[:arguments].pop.split("#").first.delete(" ").split(",")
|
91
|
+
end
|
92
|
+
@cmd[:comments] = line.split("#").slice(1..-1).join
|
93
|
+
@cmd[:comments].insert(0,"#") if !@cmd[:comments].empty?
|
94
|
+
end
|
95
|
+
|
66
96
|
end
|
67
97
|
|
68
98
|
# Obtain instruction's instance object
|
data/lib/rips/formats.rb
CHANGED
@@ -0,0 +1,22 @@
|
|
1
|
+
require "rips/formats/format"
|
2
|
+
|
3
|
+
module Rips
|
4
|
+
module Formats
|
5
|
+
|
6
|
+
class BFormat < Format
|
7
|
+
|
8
|
+
attr_reader :args
|
9
|
+
|
10
|
+
# @args: all instruction's arguments
|
11
|
+
def initialize (opcode)
|
12
|
+
super(opcode,1)
|
13
|
+
@args = {}
|
14
|
+
end
|
15
|
+
|
16
|
+
# Pass all arguments at once
|
17
|
+
def set_arguments (args)
|
18
|
+
@args[:r1] = args[0]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require "rips/formats/format"
|
2
|
+
|
3
|
+
module Rips
|
4
|
+
module Formats
|
5
|
+
|
6
|
+
class DFormat < Format
|
7
|
+
|
8
|
+
attr_reader :args
|
9
|
+
|
10
|
+
# @args: all instruction's arguments
|
11
|
+
def initialize (opcode)
|
12
|
+
super(opcode,3)
|
13
|
+
@args = {}
|
14
|
+
end
|
15
|
+
|
16
|
+
# Pass all arguments at once
|
17
|
+
def set_arguments (args)
|
18
|
+
@args[:r1], @args[:r2], @args[:r3] = args[2], args[1], args[0]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/rips/formats/format.rb
CHANGED
data/lib/rips/instructions.rb
CHANGED
@@ -0,0 +1,19 @@
|
|
1
|
+
require "rips/instructions/instruction"
|
2
|
+
|
3
|
+
module Rips
|
4
|
+
module Instructions
|
5
|
+
|
6
|
+
class Add < Instruction
|
7
|
+
|
8
|
+
attr_reader :variables, :length
|
9
|
+
|
10
|
+
# @variables: types of instruction's variables
|
11
|
+
# @length: length in bits for each variable
|
12
|
+
def initialize
|
13
|
+
super("add",Formats::DFormat.new(0b0010))
|
14
|
+
@variables = [Variables::Register.new, Variables::Register.new, Variables::Register.new]
|
15
|
+
@length = {r3:4, r2:4, r1:4}
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require "rips/instructions/instruction"
|
2
|
+
|
3
|
+
module Rips
|
4
|
+
module Instructions
|
5
|
+
|
6
|
+
class Sub < Instruction
|
7
|
+
|
8
|
+
attr_reader :variables, :length
|
9
|
+
|
10
|
+
# @variables: types of instruction's variables
|
11
|
+
# @length: length in bits for each variable
|
12
|
+
def initialize
|
13
|
+
super("sub",Formats::DFormat.new(0b0011))
|
14
|
+
@variables = [Variables::Register.new, Variables::Register.new, Variables::Register.new]
|
15
|
+
@length = {r3:4, r2:4, r1:4}
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/rips/utils.rb
CHANGED
data/lib/rips/variables.rb
CHANGED
@@ -0,0 +1,35 @@
|
|
1
|
+
require "rips/variables/variable"
|
2
|
+
|
3
|
+
module Rips
|
4
|
+
module Variables
|
5
|
+
|
6
|
+
class Address < Variable
|
7
|
+
|
8
|
+
attr_reader :syntax
|
9
|
+
|
10
|
+
# @syntax: example syntax
|
11
|
+
# @range: bit's range for variable
|
12
|
+
def initialize
|
13
|
+
super(10)
|
14
|
+
@syntax = "0..1023"
|
15
|
+
@range = [0, 2**@length-1]
|
16
|
+
end
|
17
|
+
|
18
|
+
# Check input variable syntax
|
19
|
+
def syntax? (value)
|
20
|
+
|
21
|
+
if !number?(value)
|
22
|
+
return false
|
23
|
+
end
|
24
|
+
|
25
|
+
# It should be between syntax range
|
26
|
+
if !between?(value, @range)
|
27
|
+
return false
|
28
|
+
else
|
29
|
+
return true
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -8,26 +8,26 @@ module Rips
|
|
8
8
|
attr_reader :syntax
|
9
9
|
|
10
10
|
# @syntax: example syntax
|
11
|
+
# @range: bit's range for variable
|
11
12
|
def initialize
|
12
13
|
super(8)
|
13
|
-
@syntax = "-128
|
14
|
+
@syntax = "-128..127"
|
15
|
+
@range = [-2**(@length-1), 2**(@length-1)-1]
|
14
16
|
end
|
15
17
|
|
16
18
|
# Check input variable syntax
|
17
19
|
def syntax? (value)
|
18
20
|
|
19
|
-
if !number?
|
21
|
+
if !number?(value)
|
20
22
|
return false
|
21
23
|
end
|
22
24
|
|
23
|
-
value = value.to_i
|
24
|
-
|
25
25
|
# It should be between syntax range
|
26
|
-
if !
|
26
|
+
if !between?(value, @range)
|
27
27
|
return false
|
28
28
|
else
|
29
29
|
return true
|
30
|
-
end
|
30
|
+
end
|
31
31
|
end
|
32
32
|
|
33
33
|
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require "rips/variables/variable"
|
2
|
+
|
3
|
+
module Rips
|
4
|
+
module Variables
|
5
|
+
|
6
|
+
class Port < Variable
|
7
|
+
|
8
|
+
attr_reader :syntax
|
9
|
+
|
10
|
+
# @syntax: example syntax
|
11
|
+
# @range: bit's range for variable
|
12
|
+
def initialize
|
13
|
+
super(2)
|
14
|
+
@syntax = "@0-3"
|
15
|
+
@range = [0, 2**@length-1]
|
16
|
+
end
|
17
|
+
|
18
|
+
# Check input variable syntax
|
19
|
+
def syntax? (value)
|
20
|
+
|
21
|
+
if !port?(value)
|
22
|
+
return false
|
23
|
+
end
|
24
|
+
|
25
|
+
# It should be between syntax range
|
26
|
+
if !between?(value, @range)
|
27
|
+
return false
|
28
|
+
else
|
29
|
+
return true
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -8,36 +8,26 @@ module Rips
|
|
8
8
|
attr_reader :syntax
|
9
9
|
|
10
10
|
# @syntax: example syntax
|
11
|
+
# @range: bit's range for variable
|
11
12
|
def initialize
|
12
13
|
super(4)
|
13
14
|
@syntax = "$0-15"
|
15
|
+
@range = [0, 2**@length-1]
|
14
16
|
end
|
15
17
|
|
16
18
|
# Check input variable syntax
|
17
19
|
def syntax? (value)
|
18
20
|
|
19
|
-
|
20
|
-
|
21
|
-
|
21
|
+
if !register?(value)
|
22
|
+
return false
|
23
|
+
end
|
22
24
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
# It should have only 1 or 2 digits
|
30
|
-
elsif !value.size.between?(1,2)
|
31
|
-
return false
|
32
|
-
# If it have 2 digits, first digit doesn't be zero or negative
|
33
|
-
elsif (value.size == 2) && (!value[0].match(/-|0/).nil?)
|
34
|
-
return false
|
35
|
-
# It should be between syntax range
|
36
|
-
elsif !value.to_i.between?(0, 2**@length-1)
|
37
|
-
return false
|
38
|
-
else
|
39
|
-
return true
|
40
|
-
end
|
25
|
+
# It should be between syntax range
|
26
|
+
if !between?(value, @range)
|
27
|
+
return false
|
28
|
+
else
|
29
|
+
return true
|
30
|
+
end
|
41
31
|
end
|
42
32
|
|
43
33
|
end
|
@@ -10,19 +10,29 @@ module Rips
|
|
10
10
|
@length = length
|
11
11
|
end
|
12
12
|
|
13
|
-
# Check if value is a register (
|
13
|
+
# Check if value is a register ($0..15)
|
14
14
|
def register? (value)
|
15
|
-
value[0] == "$"
|
15
|
+
(value[0] == "$") && number?(value.slice(1..-1))
|
16
16
|
end
|
17
17
|
|
18
|
+
# Check if value is a port (@0..3)
|
19
|
+
def port? (value)
|
20
|
+
(value[0] == "@") && number?(value.slice(1..-1))
|
21
|
+
end
|
22
|
+
|
18
23
|
# Check if value is a valid number (...,-1,0,1...)
|
19
24
|
def number? (value)
|
20
25
|
/\A[-]?\d+\z/ === value
|
21
26
|
end
|
22
27
|
|
28
|
+
# Check if value is between in permitted range
|
29
|
+
def between? (value, range)
|
30
|
+
to_i(value).between?(range[0], range[1])
|
31
|
+
end
|
32
|
+
|
23
33
|
# Return integer part of value
|
24
34
|
def to_i (value)
|
25
|
-
value.
|
35
|
+
number?(value) ? value.to_i : value.slice(1..-1).to_i
|
26
36
|
end
|
27
37
|
|
28
38
|
# Return error message about incorrent syntax
|
data/lib/rips/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rips
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Madh93
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-04-
|
11
|
+
date: 2015-04-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -72,15 +72,22 @@ files:
|
|
72
72
|
- lib/rips/assembler.rb
|
73
73
|
- lib/rips/error.rb
|
74
74
|
- lib/rips/formats.rb
|
75
|
+
- lib/rips/formats/aformat.rb
|
76
|
+
- lib/rips/formats/bformat.rb
|
75
77
|
- lib/rips/formats/cformat.rb
|
78
|
+
- lib/rips/formats/dformat.rb
|
76
79
|
- lib/rips/formats/format.rb
|
77
80
|
- lib/rips/instructions.rb
|
81
|
+
- lib/rips/instructions/add.rb
|
78
82
|
- lib/rips/instructions/instruction.rb
|
79
83
|
- lib/rips/instructions/li.rb
|
80
84
|
- lib/rips/instructions/set.rb
|
85
|
+
- lib/rips/instructions/sub.rb
|
81
86
|
- lib/rips/utils.rb
|
82
87
|
- lib/rips/variables.rb
|
88
|
+
- lib/rips/variables/address.rb
|
83
89
|
- lib/rips/variables/inmediate.rb
|
90
|
+
- lib/rips/variables/port.rb
|
84
91
|
- lib/rips/variables/register.rb
|
85
92
|
- lib/rips/variables/variable.rb
|
86
93
|
- lib/rips/version.rb
|