OpenCores
URL https://opencores.org/ocsvn/tinycpu/tinycpu/trunk

Subversion Repositories tinycpu

[/] [tinycpu/] [trunk/] [assembler/] [asm.rb] - Blame information for rev 38

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 36 earlz
PREFIX = "MemIn <= x\"";
2
SUFFIX = "\";";
3
SEPERATOR = "\n";
4
 
5
 
6
 
7
class OpcodeByte1
8
  attr_accessor :op, :register, :cond;
9
  def to_hex
10
    s = (op << 4 | register.number << 1 | cond).to_s(16);
11
    if s.length == 1
12
      "0"+s;
13
    elsif s.length == 0
14
      "00";
15
    else
16
      s
17
    end
18
  end
19
end
20 37 earlz
 
21 38 earlz
class OpcodeByte2
22
  attr_accessor :cond, :reg2, :useextra, :reg3;
23
  def to_hex
24
    s=(cond << 8 | reg2.number << 5 | useextra << 4 | reg3.number).to_s(16);
25
    if s.length == 1
26
      "0"+s;
27
    elsif s.length==0
28
      "00";
29
    else
30
      s;
31
    end
32
  end
33
end
34 36 earlz
 
35
 
36 35 earlz
class Register8
37
  attr_accessor :number
38
  def initialize(num)
39
        @number=num
40
  end
41
end
42 38 earlz
class OpcodeOption
43
  attr_accessor :number
44
  def initialize(num)
45
    @number=num;
46
  end
47
end
48 35 earlz
 
49 36 earlz
$iftr = 0; #0 for no condition, 1 for if TR, 2 for if not TR
50 37 earlz
$useextra = 0;
51 36 earlz
 
52 35 earlz
def mov_r8_imm8(reg,imm)
53 36 earlz
  o = OpcodeByte1.new();
54
  o.op = 0;
55
  o.register=reg;
56
  if $iftr<2 then
57
    o.cond=$iftr;
58
  else
59
    raise "if_tr_notset is not allowed with this opcode";
60
  end
61
  puts PREFIX + o.to_hex + imm.to_s(16) + SUFFIX;
62
  puts SEPERATOR;
63 35 earlz
end
64 37 earlz
def mov_rm8_imm8(reg,imm)
65
  o=OpcodeByte1.new();
66
  o.op=1;
67
  o.register=reg;
68
  if $iftr<2 then
69
    o.cond=$iftr;
70
  else
71
    raise "if_tr_notset is not allowed with this opcode";
72
  end
73
  puts PREFIX + o.to_hex + imm.to_s(16) + SUFFIX;
74
  puts SEPERATOR;
75
end
76 38 earlz
def
77 37 earlz
 
78 35 earlz
 
79
def mov(arg1,arg2)
80 37 earlz
  if arg1.kind_of? Register8 and arg2.kind_of? Integer and arg2<0x100 then
81
    mov_r8_imm8 arg1,arg2
82
  elsif arg1.kind_of? Array and arg2.kind_of? Integer and arg2<0x100 then
83
    if arg1.length>1 or arg1.length<1 then
84
      raise "memory reference is not correct. Only a register is allowed";
85
    end
86
    reg=arg1[0];
87
    mov_rm8_imm8 reg, arg2
88
  else
89
    raise "No suitable mov opcode found";
90
  end
91
 
92 35 earlz
end
93 36 earlz
def if_tr_set
94
  $iftr = 1
95
  yield
96
  $iftr = 0
97
end
98 35 earlz
 
99
 
100 36 earlz
r0=Register8.new(0)
101
r1=Register8.new(1)
102
r2=Register8.new(2)
103
r3=Register8.new(3)
104 38 earlz
r4=Register8.new(4)
105
r5=Register8.new(5)
106
sp=Register8.new(6)
107
ip=Register8.new(7)
108 36 earlz
 
109 38 earlz
 
110 36 earlz
#test code follows. Only do it here for convenience.. real usage should prefix assembly files with `require "asm.rb"`
111
if_tr_set{
112
  mov r1,0x10
113
}
114 37 earlz
mov r1,0x20
115
mov [r1], 0x50

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.