File Coverage

blib/lib/VM/Dreamer/Operation.pm
Criterion Covered Total %
statement 28 28 100.0
branch 4 4 100.0
condition 3 3 100.0
subroutine 5 5 100.0
pod 2 2 100.0
total 42 42 100.0


line stmt bran cond sub pod time code
1             package VM::Dreamer::Operation;
2              
3 2     2   14 use strict;
  2         5  
  2         68  
4 2     2   10 use warnings;
  2         3  
  2         81  
5              
6             our $VERSION = '0.851';
7              
8 2     2   1112 use VM::Dreamer::Init qw( total_width greatest_digit greatest_number init_counter );
  2         5  
  2         877  
9              
10             require Exporter;
11              
12             our @ISA = qw(Exporter);
13             our @EXPORT_OK = qw( get_new_machine add_one_to_counter );
14              
15             sub get_new_machine {
16 6     6 1 11 my ( $base, $op_code_width, $operand_width, $instruction_set ) = @_;
17              
18 6         24 my $total_width = total_width( $op_code_width, $operand_width );
19 6         17 my $greatest_digit = greatest_digit($base);
20              
21 6         49 my %machine = (
22             memory => {},
23              
24             inbox => [],
25             outbox => [],
26              
27             n_flag => 0,
28             halt => 0,
29              
30             next_instr => '',
31              
32             instruction_set => $instruction_set,
33             );
34              
35 6         21 @machine{ 'counter', 'accumulator' } = (
36             init_counter($operand_width),
37             init_counter($total_width)
38             );
39              
40 6         60 $machine{meta} = {
41             base => $base,
42             width => {
43             op_code => $op_code_width,
44             operand => $operand_width,
45             instruction => $total_width
46             },
47             greatest => {
48             digit => $greatest_digit,
49             op_code => greatest_number( $base, $op_code_width ),
50             operand => greatest_number( $base, $operand_width),
51             instruction => greatest_number( $base, $total_width )
52             }
53             };
54              
55 6         22 return \%machine;
56             }
57              
58             sub add_one_to_counter {
59 3     3 1 6 my ( $counter, $greatest_digit ) = @_;
60              
61 3         4 my $i = 0;
62 3         4 my $carry_flag = 0;
63              
64 3         13 my @little_endian_counter = reverse @$counter;
65              
66 3 100       8 if ( $little_endian_counter[0] < $greatest_digit ) {
67 1         2 $little_endian_counter[0]++;
68             }
69             else {
70 2   100     18 while ( $i <= $#little_endian_counter && $little_endian_counter[$i] == $greatest_digit ) {
71 24         28 $little_endian_counter[$i] = 0;
72 24         94 $i++;
73             }
74 2 100       6 if ( $i <= $#little_endian_counter ) {
75 1         2 $little_endian_counter[$i]++;
76             }
77             }
78              
79 3         16 return [ reverse @little_endian_counter ];
80             }
81              
82             1;
83              
84             =pod
85              
86             =head1 NAME
87              
88             VM::Dreamer::Operations - Help with the machine's operation
89              
90             =head1 SYNOPSIS
91              
92             my $machine = get_new_machine( $base, $op_code_width, $operand_width, $instruction_set );
93             add_one_to_counter( $counter, $greatest_digit );
94              
95             =head1 DESCRIPTION
96              
97             =head2 get_new_machine
98              
99             This function is called by VM::Dreamer::initialize_machine after it has validated the machine's definition.
100              
101             It sets the initial values for the machine's components as based on the machine's definition.
102              
103             It returns a reference to the newly initialized machine.
104              
105             =head2 add_one_to_counter
106              
107             Used by VM::Dreamer::increment_counter.
108              
109             Does just what it says - increments the counter by 1. It uses greatest_digit to know when to carry from one column to the next.
110              
111             For example, if the machine's greatest digit was 7 and the counter was set to 5473777, this function would return 5474000.
112              
113             Or, if the machine's greatest digit was 9 and the counter was set to 10259, this fuction would return 10260.
114              
115             It should be undefined what happens when the maximum value of the counter is passed to this function (but it just returns all zero's).
116              
117             =head1 SEE ALSO
118              
119             VM::Dreamer::initialize_machine
120             VM::Dreamer::increment_counter
121              
122             =head1 AUTHOR
123              
124             William Stevenson
125              
126             =head1 COPYRIGHT AND LICENSE
127              
128             This software is Copyright (c) 2013 by William Stevenson.
129              
130             This is free software, licensed under:
131              
132             The Artistic License 2.0 (GPL Compatible)
133            
134             =cut