DCPU16.pm

filter: Statementstmt 77.8% 172 / 221 Branchbran 61.1% 55 / 90 Conditioncond 53.8% 14 / 26 MC/DCmcdc 25.0% 4 / 16 Subroutinesub 67.4% 29 / 43 Podpod 100% 13 / 13 totaltotal 70.1% 287 / 409 CC 57 SCAR 17.7
CC
57
Cov
72%
_get_value
13.9
step
10.6
run
9.1
CRAP 5.9
« DCPU16/Disassembler.pm ↑ Summary DCPU16/Device/Console.pm »
1 package CPU::Emulator::DCPU16;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use CPU::Emulator::DCPU16::Assembler;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use CPU::Emulator::DCPU16::Disassembler;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 ▶use CPU::Emulator::DCPU16::Device::Console;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 our $VERSION = 0.3;
12 5 our $MAX_REGISTERS = 8;
13 5 our $MAX_MEMORY = 65536; # 0x10000
14  
15 =head1 NAME
16  
17 CPU::Emulator::DCPU16 - an emulator for Notch's DCPU-16 virtual CPU for the game 0x10c
18  
19 =head1 SYNOPSIS
20  
21 open(my $fh, ">:raw", $file) || die "Couldn't read file $file: $!";
22 my $program = do { local $/=undef; <$fh> };
23 $program = CPU::Emulator::DCPU16::Assembler->assemble($program) if $file =~ /\.dasm(16)?$/;
24  
25 # Create a new CPU and load a file
26 my $cpu = CPU::Emulator::DCPU16->new();
27 $cpu->load($program);
28
29 # Run it ...
30 $cpu->run;
31 # ... which is basically the same as
32 do { $cpu->step } until $cpu->halt;
33  
34 =head1 DESCRIPTION
35  
36 DCPU-16 is a spec for a virtual CPU by Notch from Mojang (of Minecraft fame).
37  
38 The spec is available here
39  
40 http://0x10c.com/doc/dcpu-16.txt
41  
42 =cut
43
44
45 =head1 METHODS
46  
47 =cut
48  
49 =head2 new
50  
51 Create a new CPU.
52  
53 =cut
54 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
55 2 my $class = shift;
56 2 my %opts = @_;
57 2 return bless \%opts, $class;
58 }
59  
60 5 ▶sub _init {
Subroutine _init: called CC=1 SCAR=0.0
61 5 my $self = shift;
62 5 $self->halt = 0;
63 5 $self->pc = 0;
64 5 $self->sp = 0xffff;
65 5 $self->o = 0;
66
67 5 $self->{_devices} = [];
68
69 # TODO these could be done with scalars and bit masks
70 $self->{_registers} = [(0x0000) x $MAX_REGISTERS],
71 5 $self->{_memory} = [(0x0000) x $MAX_MEMORY],
72
73 }
74  
75 =head2 load <program> [opt[s]]
76  
77 Load a program. Forces as re-init of the CPU.
78  
79 You can also do
80  
81 my $cpu = CPU::Emulator::DCPU16->load($program, %opts);
82
83 which is exactly the same as
84  
85 my $cpu = CPU::Emulator::DCPU16->new(%opts);
86 $cpu->load($program);
87  
88 =cut
89 5 ▶sub load {
Subroutine load: called CC=3 SCAR=11.0
Pod documented
90 5 my $self = shift;
91 5 my $bytes = shift;
92 5 my %opts = @_;
93 5 ▶ $self = $self->new(%opts) unless ref($self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 1 4 5
94 5 $self->_init;
95 5 my @bytes = $self->bytes_to_array($bytes);
96 5 ▶ die "No program was loaded\n" unless @bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless @bytes 0 5 5
97 5 $self->{_program_top} = scalar(@bytes);
98 5 splice(@{$self->{_memory}}, 0, scalar(@bytes), @bytes);
99 5 return $self;
100 }
101  
102 =head2 bytes_to_array <bytes>
103  
104 Turn a scalar of bytes into an array of words
105  
106 =cut
107 10 ▶sub bytes_to_array {
Subroutine bytes_to_array: called CC=2 SCAR=6.9
Pod documented
108 10 my $class = shift;
109 10 my $bytes = shift;
110 10 my @ret;
111 10 ▶ while (my $word = substr($bytes, 0, 2, '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $word = substr($bytes, 0, 2, '')) 113 10 123
112 113 push @ret, ord($word) * 2**8 + ord(substr($word, 1, 1));
113 }
114 10 @ret;
115 }
116  
117 =head2 map_device <class> <start address> <end address> [opt[s]]
118  
119 Map a device of the given class to these addresses
120  
121 =cut
122 1 ▶sub map_device {
Subroutine map_device: called CC=1 SCAR=0.0
Pod documented
123 1 my $self = shift;
124 1 my $dev = shift;
125 1 my $start = shift;
126 1 my $end = shift;
127 1 my %opts = @_;
128 1 push @{$self->{_devices}}, $dev->new($self->{_memory}, $start, $end, %opts);
129 1 $self->{_devices}->[-1];
130 }
131  
132 =head2 run [opt[s]]
133  
134 Run CPU until completion.
135  
136 Options available are:
137  
138 =over 4
139  
140 =item debug
141  
142 Whether or not we should print debug information and at what level.
143  
144 Default is 0 (no debug output).
145  
146 =item limit
147  
148 Maxinum number of instructions to execute.
149  
150 Default is 0 (no limit).
151  
152 =item cycle_penalty
153  
154 The time penalty for each instruction cycle in milliseconds.
155  
156 Default is 0 (no penalty)
157  
158 =item full_memory
159  
160 Allow the PC to continue past the last instruction of the program (i.e the program_top).
161  
162 This would allow programs to rewrite themselves into a larger program.
163  
164 Default is 0 (no access)
165  
166 =back
167  
168 =cut
169 5 ▶sub run {
Subroutine run: called CC=9 SCAR=22.1
Pod documented
170 5 my $self = shift;
171 5 my %opts = @_;
172 5 my $count = 1;
173 5 ▶ $opts{limit} ||= 0;
Condition100%2/2
$opts{"limit"} ||= 0
l!l
32
MC/DC100%1/1
$opts{"limit"} ||= 0
$opts{"limit"}
Truth table100%2/2
A
A= $opts{"limit"}
Aresult
11
00
174 5 ▶ $opts{debug} ||= 0;
Condition50%1/2
$opts{"debug"} ||= 0
l!l
05
MC/DC0%0/1
$opts{"debug"} ||= 0
$opts{"debug"}
Truth table50%1/2
A
A= $opts{"debug"}
Aresult
11
00
175 5 ▶ $self->_debug($self->_dump_header) if $opts{debug}>=1;
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"debug"} >= 1 0 5 5
176
177 5 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$self->halt or do { do { $self->step(%opts); $self->halt = 1 if $opts{"limit"} > 0 and ++$count > $opts{"limit"}; $self->halt = 1 if $self->pc >= $self->program_top and not $opts{"full_memory"} } } 5 730 735
178 735 $self->step(%opts);
179 735 ▶ $self->halt = 1 if $opts{limit}>0 and ++$count>$opts{limit};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"limit"} > 0 and ++$count > $opts{"limit"} 3 732 735
Condition100%3/3
$opts{"limit"} > 0 and ++$count > $opts{"limit"}
!ll&&!rl&&r
680523
MC/DC100%2/2
$opts{"limit"} > 0 and ++$count > $opts{"limit"}
$opts{"limit"} > 0 ++$count > $opts{"limit"}
Truth table100%3/3
A and B
A= $opts{"limit"} > 0B= ++$count > $opts{"limit"}
ABresult
0X0
100
111
180 735 ▶ $self->halt = 1 if $self->pc >= $self->program_top && !$opts{full_memory};
Branch: 1 branch
BranchTrueFalseTotal
if $self->pc >= $self->program_top and not $opts{"full_memory"} 2 733 735
Condition66%2/3
$self->pc >= $self->program_top and not $opts{"full_memory"}
!ll&&!rl&&r
73302
MC/DC50%1/2
$self->pc >= $self->program_top and not $opts{"full_memory"}
$self->pc >= $self->program_top not $opts{"full_memory"}
Truth table66%2/3
A and not(B)
A= $self->pc >= $self->program_topB= not $opts{"full_memory"}
ABresult
0X0
100
111
181 } until $self->halt;
182 }
183  
184 =head2 step [opt[s]]
185  
186 Run a single clock cycle of the CPU.
187  
188 Takes the same options as C<run>.
189
190 =cut
191 735 ▶sub step {
Subroutine step: called CC=10 SCAR=23.6
Pod documented
192 735 my $self = shift;
193 735 my %opts = @_;
194
195 735 ▶ $opts{debug} ||= 0;
Condition50%1/2
$opts{"debug"} ||= 0
l!l
0735
MC/DC0%0/1
$opts{"debug"} ||= 0
$opts{"debug"}
Truth table50%1/2
A
A= $opts{"debug"}
Aresult
11
00
196 735 ▶ $opts{cycle_penalty} ||= 0;
Condition50%1/2
$opts{"cycle_penalty"} ||= 0
l!l
0735
MC/DC0%0/1
$opts{"cycle_penalty"} ||= 0
$opts{"cycle_penalty"}
Truth table50%1/2
A
A= $opts{"cycle_penalty"}
Aresult
11
00
197 735 ▶ $self->_debug($self->_dump_state) if $opts{debug}>=1;
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"debug"} >= 1 0 735 735
198
199 735 my $pc = $self->pc;
200 735 my $word = $self->memory($self->pc);
201 735 ▶ die "Unknown memory at PC ".sprintf("0x%04x",$self->pc)."\n" unless defined $word;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $word 0 735 735
202 735 my $op = $word & 0x0F;
203 735 my $a = ($word >> 4) & 0x3f;
204 735 my $b = ($word >> 10) & 0x3f;
205  
206 735 $self->pc += 1;
207 735 $self->o = 0;
208
209 735 my $cost = 0;
210
211 735 my $meth;
212 # Basic opcodes
213 735 ▶ if ($op) {
Branch: 2 branches
BranchTrueFalseTotal
if ($op) { } 734 1 735
elsif ($a == 1) { } 1 0 1
214 734 $meth = qw(NOOP _SET _ADD _SUB _MUL _DIV _MOD _SHL _SHR _AND _BOR _XOR _IFE _IFN _IFG _IFB)[$op];
215 734 ▶ die "Illegal opcode $op\n" unless defined $meth;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $meth 0 734 734
216 # Defined non-basic opcodes
217 } elsif ($a == 0x01) {
218 1 $meth = "_JSR";
219 # Reserved non-basic opcodes
220 } else {
221 0 die "Illegal extended opcode $a\n";
222 }
223  
224 735 my $aa = $self->_get_value($a, \$cost);
225 735 my $bb = $self->_get_value($b, \$cost);
226
227 735 $self->$meth($aa, $bb, \$cost);
228 735 ▶ select(undef, undef, undef, $cost*$opts{cycle_penalty}/1000) if $opts{cycle_penalty}>0;
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"cycle_penalty"} > 0 0 735 735
229 735 $_->tick for @{$self->{_devices}};
230 735 return $cost;
231 }
232  
233 =head1 METHODS TO GET THE STATE OF THE CPU
234  
235 =head2 pc
236  
237 The current program counter.
238  
239 =cut
240 3328 ▶sub pc : lvalue {
Subroutine pc: called CC=2 SCAR=7.1
Pod documented
241 3328 my $self = shift;
242 3328 ▶ $self->{_pc} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 3328 3328
243 3328 $self->{_pc};
244 }
245  
246 =head2 sp
247  
248 The current stack pointer.
249  
250 =cut
251 7 ▶sub sp : lvalue {
Subroutine sp: called CC=2 SCAR=7.1
Pod documented
252 7 my $self = shift;
253 7 ▶ $self->{_sp} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 7 7
254 7 $self->{_sp};
255 }
256  
257 =head2 o
258  
259 The current overflow.
260  
261 =cut
262 1008 ▶sub o : lvalue {
Subroutine o: called CC=2 SCAR=7.1
Pod documented
263 1008 my $self = shift;
264 1008 ▶ $self->{_o} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 1008 1008
265 1008 $self->{_o};
266 }
267  
268 =head2 halt [halt state]
269  
270 Halt the CPU or check to see whether it's halted.
271  
272 =cut
273 745 ▶sub halt : lvalue {
Subroutine halt: called CC=2 SCAR=7.1
Pod documented
274 745 my $self = shift;
275 745 ▶ $self->{_halt} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 745 745
276 745 $self->{_halt};
277 }
278  
279 =head2 program_top
280  
281 The address of the first memory location after the loaded program.
282  
283 =cut
284 735 ▶sub program_top : lvalue {
Subroutine program_top: called CC=2 SCAR=7.1
Pod documented
285 735 my $self = shift;
286 735 ▶ $self->{_program_top} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 735 735
287 735 $self->{_program_top};
288 }
289  
290 =head2 register <location>
291  
292 Get or set the value of a register.
293  
294 =cut
295 110 ▶sub register : lvalue {
Subroutine register: called CC=5 SCAR=17.8
Pod documented
296 110 my $self = shift;
297 110 ▶ return $self->{_registers} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 110 110
298 110 ▶ my $loc = shift; die "Invalid register $loc at pc ".$self->pc." (".sprintf("%02x", $self->pc).")\n" if $loc<0 || $loc>=$MAX_REGISTERS;
Branch: 1 branch
BranchTrueFalseTotal
if $loc < 0 or $loc >= $CPU::Emulator::DCPU16::MAX_REGISTERS 0 110 110
Condition33%1/3
$loc < 0 or $loc >= $CPU::Emulator::DCPU16::MAX_REGISTERS
l!l&&r!l&&!r
00110
MC/DC0%0/2
$loc < 0 or $loc >= $CPU::Emulator::DCPU16::MAX_REGISTERS
$loc < 0 $loc >= $CPU::Emulator::DCPU16::MAX_REGISTERS
Truth table33%1/3
A or B
A= $loc < 0B= $loc >= $CPU::Emulator::DCPU16::MAX_REGISTERS
ABresult
1X1
011
000
299 110 ▶ $self->{_registers}[$loc] = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 110 110
300 110 $self->{_registers}[$loc];
301 }
302 # TODO ugly
303 794 ▶sub _reg_ref {
Subroutine _reg_ref: called CC=3 SCAR=12.0
304 794 my $self = shift;
305 794 ▶ my $loc = shift; die "Invalid register $loc at pc ".$self->pc." (".sprintf("%02x", $self->pc).")\n" if $loc<0 || $loc>=$MAX_REGISTERS;
Branch: 1 branch
BranchTrueFalseTotal
if $loc < 0 or $loc >= $CPU::Emulator::DCPU16::MAX_REGISTERS 0 794 794
Condition33%1/3
$loc < 0 or $loc >= $CPU::Emulator::DCPU16::MAX_REGISTERS
l!l&&r!l&&!r
00794
MC/DC0%0/2
$loc < 0 or $loc >= $CPU::Emulator::DCPU16::MAX_REGISTERS
$loc < 0 $loc >= $CPU::Emulator::DCPU16::MAX_REGISTERS
Truth table33%1/3
A or B
A= $loc < 0B= $loc >= $CPU::Emulator::DCPU16::MAX_REGISTERS
ABresult
1X1
011
000
306 794 \($self->{_registers}[$loc]);
307 }
308  
309 =head2 memory <location>
310  
311 Get or set the value of a memory location.
312  
313 =cut
314 755 ▶sub memory : lvalue {
Subroutine memory: called CC=5 SCAR=17.8
Pod documented
315 755 my $self = shift;
316 755 ▶ return $self->{_memory} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 755 755
317 755 my $loc = shift;
318 755 ▶ die "Invalid memory $loc at pc ".$self->pc." (".sprintf("%02x", $self->pc).")\n" if $loc<0 || $loc>=$MAX_MEMORY;
Branch: 1 branch
BranchTrueFalseTotal
if $loc < 0 or $loc >= $CPU::Emulator::DCPU16::MAX_MEMORY 0 755 755
Condition33%1/3
$loc < 0 or $loc >= $CPU::Emulator::DCPU16::MAX_MEMORY
l!l&&r!l&&!r
00755
MC/DC0%0/2
$loc < 0 or $loc >= $CPU::Emulator::DCPU16::MAX_MEMORY
$loc < 0 $loc >= $CPU::Emulator::DCPU16::MAX_MEMORY
Truth table33%1/3
A or B
A= $loc < 0B= $loc >= $CPU::Emulator::DCPU16::MAX_MEMORY
ABresult
1X1
011
000
319 755 ▶ $self->{_memory}[$loc] = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 755 755
320 755 $self->{_memory}[$loc];
321 }
322 # TODO ugly
323 464 ▶sub _mem_ref {
Subroutine _mem_ref: called CC=3 SCAR=12.0
324 464 my $self = shift;
325 464 my $loc = shift;
326 464 ▶ die "Invalid memory $loc at pc ".$self->pc." (".sprintf("%02x", $self->pc).")\n" if $loc<0 || $loc>=$MAX_MEMORY;
Branch: 1 branch
BranchTrueFalseTotal
if $loc < 0 or $loc >= $CPU::Emulator::DCPU16::MAX_MEMORY 0 464 464
Condition33%1/3
$loc < 0 or $loc >= $CPU::Emulator::DCPU16::MAX_MEMORY
l!l&&r!l&&!r
00464
MC/DC0%0/2
$loc < 0 or $loc >= $CPU::Emulator::DCPU16::MAX_MEMORY
$loc < 0 $loc >= $CPU::Emulator::DCPU16::MAX_MEMORY
Truth table33%1/3
A or B
A= $loc < 0B= $loc >= $CPU::Emulator::DCPU16::MAX_MEMORY
ABresult
1X1
011
000
327 464 \($self->{_memory}[$loc]);
328 }
329  
330 0 ▶sub _dump_header {
Subroutine _dump_header: not called CC=1 SCAR=6.9
331 0 "PC SP OV A B C X Y Z I J Instruction\n".
332 "---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- -----------";
333 }
334  
335 0 ▶sub _dump_state {
Subroutine _dump_state: not called CC=1 SCAR=6.9
336 0 my $self = shift;
337 sprintf("%04x %04x %04x %04x %04x %04x %04x %04x %04x %04x %04x %s",
338 $self->pc, $self->sp, $self->o,
339 $self->register(0), $self->register(1), $self->register(2), $self->register(3),
340 $self->register(4), $self->register(5), $self->register(6), $self->register(7),
341 0 CPU::Emulator::DCPU16::Disassembler->disassemble($self->pc, @{$self->memory}));
342 }
343  
344 0 ▶sub _debug {
Subroutine _debug: not called CC=1 SCAR=6.9
345 0 my $self = shift;
346 0 my $mess = shift;
347 0 print "$mess\n";
348 }
349  
350 1470 ▶sub _get_value {
Subroutine _get_value: called CC=13 SCAR=26.3
351 1470 my $self = shift;
352 1470 my $value = shift;
353 1470 my $cost = shift;
354 1470 my $ret;
355 1470 ▶ if ($value < 0x08) {
Branch: 11 branches
BranchTrueFalseTotal
if ($value < 8) { } 794 676 1470
elsif ($value < 16) { } 94 582 676
elsif ($value < 24) { } 10 572 582
elsif ($value == 24) { } 1 571 572
elsif ($value == 25) { } 0 571 571
elsif ($value == 26) { } 0 571 571
elsif ($value == 27) { } 0 571 571
elsif ($value == 28) { } 97 474 571
elsif ($value == 29) { } 0 474 474
elsif ($value == 30) { } 4 470 474
elsif ($value == 31) { } 355 115 470
356 794 $ret = $self->_reg_ref($value);
357 } elsif ($value < 0x10) {
358 94 $ret = $self->_mem_ref($self->register($value & 7));
359 } elsif ($value < 0x18) {
360 10 $$cost += 1;
361 10 my $next = $self->memory($self->pc++);
362 10 $ret = $self->_mem_ref($next + $self->register($value & 7) & 0xffff);
363 } elsif ($value == 0x18) {
364 1 $ret = $self->_mem_ref($self->sp++);
365 } elsif ($value == 0x19) {
366 0 $ret = $self->_mem_ref($self->sp);
367 } elsif ($value == 0x1A) {
368 0 $ret = $self->_mem_ref($self->sp--);
369 } elsif ($value == 0x1B) {
370 0 $ret = \($self->{_sp});
371 } elsif ($value == 0x1C) {
372 97 $ret = \($self->{_pc});
373 } elsif ($value == 0x1D) {
374 0 $ret = \($self->{_o});
375 } elsif ($value == 0x1E) {
376 4 $$cost += 1;
377 4 $ret = $self->_mem_ref($self->memory($self->pc++));
378 } elsif ($value == 0x1F) {
379 355 $$cost += 1;
380 355 $ret = $self->_mem_ref($self->pc++);
381 } else {
382 115 $ret = ($value - 0x20)
383 }
384 1470 ▶ return ref($ret) ? $ret : \$ret;
Branch: 1 branch
BranchTrueFalseTotal
ref $ret ? : 1355 115 1470
385 }
386  
387 5 our %_skiptable = (0x10 => 1, 0x11 => 1, 0x12 => 1, 0x13 => 1, 0x14 => 1, 0x15 => 1, 0x1E => 1, 0x1F => 1);
388 4 ▶sub _skip {
Subroutine _skip: called CC=2 SCAR=7.0
389 4 my $self = shift;
390 4 my $cost = shift;
391 4 $$cost++;
392 4 my $op = $self->memory($self->pc++);
393 4 $self->pc += $_skiptable{$op >> 10};
394 4 ▶ $self->pc += $_skiptable{($op >> 4) & 31} if (($op & 0x0F) == 0);
Branch: 1 branch
BranchTrueFalseTotal
if ($op & 15) == 0 0 4 4
395 }
396  
397 0 ▶sub _NOOP {
Subroutine _NOOP: not called CC=1 SCAR=0.0
398 # Just what it says on the tin
399 }
400  
401 1 ▶sub _JSR {
Subroutine _JSR: called CC=1 SCAR=0.0
402 1 my ($self, $a, $b, $cost) = @_;
403 1 $$cost += 2;
404 1 $self->memory(--$self->sp) = $self->pc;
405 1 $self->pc = $$b;
406  
407 }
408  
409 # 0x1: SET a, b - sets a to b
410 368 ▶sub _SET {
Subroutine _SET: called CC=1 SCAR=0.0
411 368 my ($self, $a, $b, $cost) = @_;
412 368 $$cost += 1;
413 368 $$a = $$b;
414 }
415  
416 # 0x2: ADD a, b - sets a to a+b, sets O to 0x0001 if there's an overflow, 0x0 otherwise
417 255 ▶sub _ADD {
Subroutine _ADD: called CC=1 SCAR=0.0
418 255 my ($self, $a, $b, $cost) = @_;
419 255 $$cost += 2;
420 255 $$a += $$b;
421 255 $self->o = $$a >> 16;
422 }
423  
424 # 0x3: SUB a, b - sets a to a-b, sets O to 0xffff if there's an underflow, 0x0 otherwise
425 12 ▶sub _SUB {
Subroutine _SUB: called CC=1 SCAR=0.0
426 12 my ($self, $a, $b, $cost) = @_;
427 12 $$cost += 2;
428 12 $$a -= $$b;
429 12 $self->o = $$a >> 16;
430 }
431  
432 # 0x4: MUL a, b - sets a to a*b, sets O to ((a*b)>>16)&0xffff
433 0 ▶sub _MUL {
Subroutine _MUL: not called CC=1 SCAR=6.9
434 0 my ($self, $a, $b, $cost) = @_;
435 0 $$cost += 2;
436 0 $$a *= $$b;
437 0 $self->o = $$a >> 16;
438 }
439  
440 # 0x5: DIV a, b - sets a to a/b, sets O to ((a<<16)/b)&0xffff. if b==0, sets a and O to 0 instead.
441 0 ▶sub _DIV {
Subroutine _DIV: not called CC=2 SCAR=17.9
442 0 my ($self, $a, $b, $cost) = @_;
443 0 $$cost += 3;
444 0 ▶ if ($$b) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$b) { } 0 0 0
445 0 $$a /= $$b;
446 } else {
447 0 $$a = 0;
448 }
449 0 $self->o = $$a >> 16;
450 }
451  
452 # 0x6: MOD a, b - sets a to a%b. if b==0, sets a to 0 instead.
453 0 ▶sub _MOD {
Subroutine _MOD: not called CC=2 SCAR=17.9
454 0 my ($self, $a, $b, $cost) = @_;
455 0 $$cost += 3;
456 0 ▶ if ($$b) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$b) { } 0 0 0
457 0 $$a %= $$b;
458 } else {
459 0 $$a = 0;
460 }
461 }
462  
463 # 0x7: SHL a, b - sets a to a<<b, sets O to ((a<<b)>>16)&0xffff
464 1 ▶sub _SHL {
Subroutine _SHL: called CC=1 SCAR=0.0
465 1 my ($self, $a, $b, $cost) = @_;
466 1 $$cost += 2;
467 1 $$a <<= $$b;
468 1 $self->o = $$a >> 16;
469 }
470  
471 # 0x8: SHR a, b - sets a to a>>b, sets O to ((a<<16)>>b)&0xffff
472 0 ▶sub _SHR {
Subroutine _SHR: not called CC=1 SCAR=6.9
473 0 my ($self, $a, $b, $cost) = @_;
474 0 $$cost += 2;
475 0 $$a >>= $$b;
476 0 $self->o = $$a >> 16;
477 }
478  
479 # 0x9: AND a, b - sets a to a&b
480 0 ▶sub _AND {
Subroutine _AND: not called CC=1 SCAR=6.9
481 0 my ($self, $a, $b, $cost) = @_;
482 0 $$cost += 1;
483 0 $$a &= $$b;
484 }
485  
486 # 0xa: BOR a, b - sets a to a|b
487 0 ▶sub _BOR {
Subroutine _BOR: not called CC=1 SCAR=6.9
488 0 my ($self, $a, $b, $cost) = @_;
489 0 $$cost += 1;
490 0 $$a |= $b;
491 }
492  
493 # 0xb: XOR a, b - sets a to a^b
494 0 ▶sub _XOR {
Subroutine _XOR: not called CC=1 SCAR=6.9
495 0 my ($self, $a, $b, $cost) = @_;
496 0 $$cost += 1;
497 0 $$a ^= $b;
498 }
499  
500 # 0xc: IFE a, b - performs next instruction only if a==b
501 0 ▶sub _IFE {
Subroutine _IFE: not called CC=2 SCAR=17.9
502 0 my ($self, $a, $b, $cost) = @_;
503 0 $$cost += 2;
504 0 ▶ $self->_skip($cost) unless $$a+0 == $$b+0;
Branch: 1 branch
BranchTrueFalseTotal
unless $$a + 0 == $$b + 0 0 0 0
505 }
506  
507 # 0xd: IFN a, b - performs next instruction only if a!=b
508 98 ▶sub _IFN {
Subroutine _IFN: called CC=2 SCAR=6.9
509 98 my ($self, $a, $b, $cost) = @_;
510 98 $$cost += 2;
511 98 ▶ $self->_skip($cost) unless $$a+0 != $$b+0;
Branch: 1 branch
BranchTrueFalseTotal
unless $$a + 0 != $$b + 0 4 94 98
512 }
513  
514 # 0xe: IFG a, b - performs next instruction only if a>b
515 0 ▶sub _IFG {
Subroutine _IFG: not called CC=2 SCAR=17.9
516 0 my ($self, $a, $b, $cost) = @_;
517 0 $$cost += 2;
518 0 ▶ $self->_skip($cost) unless $$a+0 > $$b+0;
Branch: 1 branch
BranchTrueFalseTotal
unless $$a + 0 > $$b + 0 0 0 0
519 }
520  
521 # 0xf: IFB a, b - performs next instruction only if (a&b)!=0
522 0 ▶sub _IFB {
Subroutine _IFB: not called CC=2 SCAR=17.9
523 0 my ($self, $a, $b, $cost) = @_;
524 0 $$cost += 2;
525 0 ▶ $self->_skip($cost) unless ($$a+0 & $$b+0) != 0;
Branch: 1 branch
BranchTrueFalseTotal
unless ($$a + 0 & $$b + 0) != 0 0 0 0
526 }
527  
528  
529 =head1 SEE ALSO
530  
531 L<CPU::Emulator::DCPU16::Assembler>
532  
533 L<CPU::Emulator::DCPU16::Disassembler>
534  
535 =head1 ACKNOWLEDGEMENTS
536  
537 Implementation inspiration came from:
538  
539 =over 4
540  
541 =item Matt Bell's Javascript implementation (https://github.com/mappum/DCPU-16)
542  
543 =item Brian Swetland's C Implementation (https://github.com/swetland/dcpu16)
544  
545 =item Jesse Luehrs's Perl Implementation (https://github.com/doy/games-emulation-dcpu16)
546  
547 =back
548  
549 =head1 AUTHOR
550  
551 Simon Wistow <simon@thegestalt.org>
552  
553 =head1 COPYRIGHT
554  
555 Copyright 2011 - Simon Wistow
556  
557 Released under the same terms as Perl itself.
558  
559 =head1 DEVELOPMENT
560  
561 Latest development version available from
562  
563 https://github.com/simonwistow/CPU-Emulator-DCPU16
564  
565 =cut
566  
567 5 1;
« DCPU16/Disassembler.pm ↑ Summary DCPU16/Device/Console.pm »