rips 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f9d9774b257b7691b23c8eeb4fcd0b9de10f2515
4
- data.tar.gz: 9fff56fe4e0eacebfd306b9d4cb4b1dc9a954fe0
3
+ metadata.gz: cf8c8bebaebfc51d045b029332f2cd2e061a88bf
4
+ data.tar.gz: 308c2b211e7feb112d7bbe80e3532771004efbf1
5
5
  SHA512:
6
- metadata.gz: cfd2de7a53fe4840c1b66c9002c59b8e04f75473471b6fe80020c9b82f79c0c320dd33a8ba9d3f2860804eb1aef16837802c3991647d3e4ec3b7517c3d59a5a0
7
- data.tar.gz: aef9b647fd23285e73e1de83dff021551a2fe042fa2af82c38e4f81c20ee0cd053fe3c4368221a7fe5151dbfdd95294453b1be16ef4a9b696eeda742210bb281
6
+ metadata.gz: efa9aa5de99297bee011ab544cf5d19d7759750141f022fa14c73a3d89b799fe77cba869fc1fa1e8302a0d924bb2ba8bbad267226043aca648940e28e622b6e5
7
+ data.tar.gz: ebb7b52fae71dfa37e44ae6ba8bccca459343567fbda81d5354c5eec09a653a07019afdb8946611f7faa4af933b70b53a1bc9a5c9e48abc513753123784dd349
data/.gitignore CHANGED
@@ -12,3 +12,5 @@
12
12
  *.o
13
13
  *.a
14
14
  mkmf.log
15
+ *.input
16
+ *.dat
@@ -1,3 +1,10 @@
1
- li $1, 12
2
- li $2, 5
3
- li $2, 1
1
+ #***************#
2
+ # Example #
3
+ #***************#
4
+
5
+ # Output in: progfile.dat
6
+
7
+ li $1, 12 # Load 12 in Register 1
8
+ li $2, -5 # Load -5 in Register 2
9
+
10
+ add $3, $1, $2 # Reg3 = Reg1 + Reg2
@@ -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 ARGV.empty?
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|
@@ -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
- parse_input(line)
30
- exists_instruction
31
+ # If line is empty -> next line
32
+ if !line.empty?
31
33
 
32
- @instruction = get_instruction
33
-
34
- argument_size
35
- argument_syntax
34
+ parse_input(line)
35
+ @instruction = nil
36
36
 
37
- @instruction.set_arguments(@cmd[:arguments])
37
+ # If it's a comment -> show but not work with it
38
+ if line[0] != "#"
38
39
 
39
- show
40
- @output << @instruction.code
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
- # Show code with '_' separator
50
- puts "Code Instruction: " << @instruction.code.scan(/.{4}|.+/).join("_")
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
- @cmd = { name: line.split(' ').first.downcase,
65
- arguments: line.split(' ',2).pop.delete(' ').split(',') }
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
@@ -1 +1,4 @@
1
- require "rips/formats/cformat"
1
+ require "rips/formats/aformat"
2
+ require "rips/formats/bformat"
3
+ require "rips/formats/cformat"
4
+ require "rips/formats/dformat"
@@ -0,0 +1,18 @@
1
+ require "rips/formats/format"
2
+
3
+ module Rips
4
+ module Formats
5
+
6
+ class AFormat < Format
7
+
8
+ attr_reader :args
9
+
10
+ # @args: all instruction's arguments
11
+ def initialize (opcode)
12
+ super(opcode,0)
13
+ @args = {}
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -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
@@ -12,6 +12,11 @@ module Rips
12
12
  def initialize (opcode, args_number)
13
13
  @opcode, @args_number = opcode, args_number
14
14
  end
15
+
16
+ # Pass all arguments at once
17
+ def set_arguments (args)
18
+ end
19
+
15
20
  end
16
21
  end
17
22
  end
@@ -1 +1,3 @@
1
- require "rips/instructions/li"
1
+ require "rips/instructions/add"
2
+ require "rips/instructions/li"
3
+ require "rips/instructions/sub"
@@ -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
@@ -1,6 +1,6 @@
1
1
  module Rips
2
2
  module Instructions
3
3
 
4
- SET = ["li"]
4
+ SET = ["add", "li", "sub"]
5
5
  end
6
6
  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
@@ -1,6 +1,9 @@
1
1
  class Integer
2
2
  def to_bin(width)
3
- num = '%0*b' % [width, self]
4
- num.gsub('.','1')
3
+ if self < 0
4
+ '%0*b' % [width, (2**width-1) - ~self]
5
+ else
6
+ '%0*b' % [width, self]
7
+ end
5
8
  end
6
9
  end
@@ -1,2 +1,4 @@
1
- require "rips/variables/register"
2
- require "rips/variables/inmediate"
1
+ require "rips/variables/address"
2
+ require "rips/variables/inmediate"
3
+ require "rips/variables/port"
4
+ require "rips/variables/register"
@@ -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...127"
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? (value)
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 !value.to_i.between?(-2**(@length-1),2**(@length-1)-1)
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
- if !register? (value)
20
- return false
21
- end
21
+ if !register?(value)
22
+ return false
23
+ end
22
24
 
23
- # Get integer part
24
- value = value.split('$',2).last
25
-
26
- # If contains more "$"
27
- if value.include? "$"
28
- return false
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.split('$',2).last.to_i
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
@@ -1,3 +1,3 @@
1
1
  module Rips
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
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
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-05 00:00:00.000000000 Z
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