| 1 | # $Id$ | ||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||
| 3 | package CPU::Z80::Assembler::Opcode; | ||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||
| 5 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||
| 9 | CPU::Z80::Assembler::Opcode - Represents one assembly expression to be computed at link time | ||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||
| 11 | =cut | ||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||
| 13 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||
| 15 | 36 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 16 | 36 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||
| 18 | 36 | our $VERSION = '2.24'; | |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||
| 20 | 36 | ▶ | use Asm::Preproc::Line; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||
| 22 | sub new { | ||||||||||||||||||||||||||||||
| 23 | 25345 | ▶ | my($class, %args) = @_; | ||||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||||
| 24 | bless [ | ||||||||||||||||||||||||||||||
| 25 | $args{address}, # address where loaded | ||||||||||||||||||||||||||||||
| 26 | $args{line} || Asm::Preproc::Line->new(), | ||||||||||||||||||||||||||||||
| 27 | # line where tokens found | ||||||||||||||||||||||||||||||
| 28 | 25345 | ▶ | $args{child} || [], # list of children of this node | ||||||||||||||||||||||||||||
|
Condition80%4/5
$args{'line'} || 'Asm::Preproc::Line'->new
$args{'child'} || []
MC/DC0%0/2
$args{'line'} || 'Asm::Preproc::Line'->new
$args{'line'}
'Asm::Preproc::Line'->new
MC/DC100%1/1
$args{'child'} || []
$args{'child'}
Truth table66%2/3
A || B
A= $args{'line'}B= 'Asm::Preproc::Line'->new
Truth table100%2/2
A
A= $args{'child'}
| |||||||||||||||||||||||||||||||
| 29 | # each child is a byte value or an expression | ||||||||||||||||||||||||||||||
| 30 | # to compute the byte(s) | ||||||||||||||||||||||||||||||
| 31 | ], $class; | ||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||
| 33 | 88754 | ▶ | sub address { defined($_[1]) ? $_[0][0] = $_[1] : $_[0][0] } | ||||||||||||||||||||||||||||
|
Subroutine
address: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 34 | 5420 | ▶ | sub line { defined($_[1]) ? $_[0][1] = $_[1] : $_[0][1] } | ||||||||||||||||||||||||||||
|
Subroutine
line: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 35 | 102131 | ▶ | sub child { defined($_[1]) ? $_[0][2] = $_[1] : $_[0][2] } | ||||||||||||||||||||||||||||
|
Subroutine
child: called CC=2 SCAR=7.6
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||
| 37 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||
| 39 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||
| 41 | use CPU::Z80::Assembler::Opcode; | ||||||||||||||||||||||||||||||
| 42 | $opcode = CPU::Z80::Assembler::Opcode->new( | ||||||||||||||||||||||||||||||
| 43 | address => 0, | ||||||||||||||||||||||||||||||
| 44 | line => $line, | ||||||||||||||||||||||||||||||
| 45 | child => [byte, byte, ["type", $expr]]); | ||||||||||||||||||||||||||||||
| 46 | $value = $opcode->evaluate; | ||||||||||||||||||||||||||||||
| 47 | $bytes = $opcode->bytes($address, \%symbol_table); | ||||||||||||||||||||||||||||||
| 48 | $size = $opcode->size; | ||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||
| 50 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||
| 52 | This module defines the class that represents one assembly instruction to be | ||||||||||||||||||||||||||||||
| 53 | added to the object code. The instruction can contain references to | ||||||||||||||||||||||||||||||
| 54 | L<CPU::Z80::Assembler::Expr|CPU::Z80::Assembler::Expr> expressions that are computed at link time. | ||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||
| 56 | =head1 EXPORTS | ||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||
| 58 | Nothing. | ||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||
| 60 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||
| 62 | =head2 new | ||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||
| 64 | Creates a new object. | ||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||
| 66 | =head2 address | ||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||
| 68 | Address where this opcode is loaded, computed at link time. | ||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||
| 70 | =head2 child | ||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||
| 72 | The number of children is the number of bytes stored in the object code for | ||||||||||||||||||||||||||||||
| 73 | this instruction. | ||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||
| 75 | Each child is either a scalar containing the byte value to be added to the object code, | ||||||||||||||||||||||||||||||
| 76 | or a L<CPU::Z80::Assembler::Expr|CPU::Z80::Assembler::Expr> expression to be evaluated at link time. In case | ||||||||||||||||||||||||||||||
| 77 | of a word expression, then a special undef value is used as a placeholder to keep | ||||||||||||||||||||||||||||||
| 78 | the C<child> list the correct size. | ||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||
| 80 | =head2 line | ||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||
| 82 | Get/set the line - text, file name and line number where the token was read. | ||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||
| 84 | =cut | ||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||
| 86 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||
| 88 | =head2 evaluate | ||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||
| 90 | Called when opcode is referred to by a label, returns the opcode address. | ||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||
| 92 | =cut | ||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||
| 94 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||
| 96 | 8662 | ▶ | sub evaluate { my($self) = @_; | ||||||||||||||||||||||||||||
|
Subroutine
evaluate: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 97 | 8662 | return $self->address; | |||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||
| 100 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||
| 102 | =head2 bytes | ||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||
| 104 | $bytes = $opcode->bytes($address, \%symbol_table); | ||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||
| 106 | Computes all the expressions in C<child> and returns the bytes string | ||||||||||||||||||||||||||||||
| 107 | with the result object code. | ||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||
| 109 | =cut | ||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||
| 111 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||
| 113 | sub bytes { | ||||||||||||||||||||||||||||||
| 114 | 23735 | ▶ | my($self, $address, $symbol_table) = @_; | ||||||||||||||||||||||||||||
|
Subroutine
bytes: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||
| 116 | 23735 | my $bytes = ""; | |||||||||||||||||||||||||||||
| 117 | 23735 | for my $expr (@{$self->child}) { | |||||||||||||||||||||||||||||
| 118 | 74391 | ▶ | if (! defined($expr)) { | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 119 | # skip undefined values - used as placeholder for the second byte of a word | ||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||
| 121 | elsif (ref($expr)) { | ||||||||||||||||||||||||||||||
| 122 | 12568 | $bytes .= $expr->bytes($address, $symbol_table); | |||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||
| 124 | else { | ||||||||||||||||||||||||||||||
| 125 | 59029 | $bytes .= pack("C", $expr & 0xFF); | |||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||
| 128 | 23735 | return $bytes; | |||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||
| 131 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||
| 133 | =head2 size | ||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||
| 135 | $size = $opcode->size; | ||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||
| 137 | Return the number of bytes that this opcode will generate. | ||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||
| 139 | =cut | ||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||
| 141 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||
| 143 | 74378 | ▶ | sub size { my($self) = @_; | ||||||||||||||||||||||||||||
|
Subroutine
size: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 144 | 74378 | return scalar(@{$self->child}); | |||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||
| 147 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||
| 149 | =head1 BUGS and FEEDBACK | ||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||
| 151 | See L<CPU::Z80::Assembler|CPU::Z80::Assembler>. | ||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||
| 153 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||
| 155 | L<CPU::Z80::Assembler|CPU::Z80::Assembler> | ||||||||||||||||||||||||||||||
| 156 | L<Asm::Preproc::Line|Asm::Preproc::Line> | ||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||
| 158 | =head1 AUTHORS, COPYRIGHT and LICENCE | ||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||
| 160 | See L<CPU::Z80::Assembler|CPU::Z80::Assembler>. | ||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||
| 162 | =cut | ||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||
| 164 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||
| 166 | 36 | 1; | |||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||