| 1 | package VM::Dreamer::Operation; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = '0.851'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use VM::Dreamer::Init qw( total_width greatest_digit greatest_number init_counter ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | require Exporter; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 13 | 2 | our @EXPORT_OK = qw( get_new_machine add_one_to_counter ); | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 6 | ▶ | sub get_new_machine { | ||||||||||||||||||||||||||
|
Subroutine
get_new_machine: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | 6 | my ( $base, $op_code_width, $operand_width, $instruction_set ) = @_; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 6 | my $total_width = total_width( $op_code_width, $operand_width ); | |||||||||||||||||||||||||||
| 19 | 6 | my $greatest_digit = greatest_digit($base); | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 6 | 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 | @machine{ 'counter', 'accumulator' } = ( | |||||||||||||||||||||||||||
| 36 | init_counter($operand_width), | ||||||||||||||||||||||||||||
| 37 | init_counter($total_width) | ||||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | $machine{meta} = { | ||||||||||||||||||||||||||||
| 41 | 6 | 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 | return \%machine; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 3 | ▶ | sub add_one_to_counter { | ||||||||||||||||||||||||||
|
Subroutine
add_one_to_counter: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | 3 | my ( $counter, $greatest_digit ) = @_; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 3 | my $i = 0; | |||||||||||||||||||||||||||
| 62 | 3 | my $carry_flag = 0; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 3 | my @little_endian_counter = reverse @$counter; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 3 | ▶ | if ( $little_endian_counter[0] < $greatest_digit ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 1 | $little_endian_counter[0]++; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | else { | ||||||||||||||||||||||||||||
| 70 | 2 | ▶ | while ( $i <= $#little_endian_counter && $little_endian_counter[$i] == $greatest_digit ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$i <= $#little_endian_counter and $little_endian_counter[$i] == $greatest_digit
MC/DC100%2/2
$i <= $#little_endian_counter and $little_endian_counter[$i] == $greatest_digit
$i <= $#little_endian_counter
$little_endian_counter[$i] == $greatest_digit
Truth table100%3/3
A and B
A= $i <= $#little_endian_counterB= $little_endian_counter[$i] == $greatest_digit
| |||||||||||||||||||||||||||||
| 71 | 24 | $little_endian_counter[$i] = 0; | |||||||||||||||||||||||||||
| 72 | 24 | $i++; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 2 | ▶ | if ( $i <= $#little_endian_counter ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 1 | $little_endian_counter[$i]++; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 3 | return [ reverse @little_endian_counter ]; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 2 | 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 <william at coders dot coop> | ||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||