| 1 | # $Id$ | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package CPU::Z80::Assembler::List; | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | CPU::Z80::Assembler::List - Assembly listing output class | ||||||||||||||||||||||||||||
| 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 | ▶ | use Text::Tabs; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 36 | ▶ | use Iterator::Simple::Lookahead; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 36 | our $VERSION = '2.23'; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | use Class::Struct ( | ||||||||||||||||||||||||||||
| 24 | 36 | output => '$', # output file handle for the list | |||||||||||||||||||||||||||
| 25 | input => '$', # input lines or iterators passed to z80asm | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | _line_stream => '$', # input line stream with whole program | ||||||||||||||||||||||||||||
| 28 | _address => '$', # output address | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | _current_line => '$', # line of the current opcode(s) | ||||||||||||||||||||||||||||
| 31 | _current_address => '$', # address of the current opcode(s) | ||||||||||||||||||||||||||||
| 32 | _current_bytes => '$', # all bytes of all opcodes of _current_line | ||||||||||||||||||||||||||||
| 33 | 36 | ▶ | ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | use CPU::Z80::Assembler::List; | ||||||||||||||||||||||||||||
| 40 | my $lst = CPU::Z80::Assembler::List->new(input => $asm_input, output => \*STDOUT); | ||||||||||||||||||||||||||||
| 41 | $lst->add($line, $address, $bytes); | ||||||||||||||||||||||||||||
| 42 | $lst->flush(); | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | This module handles the output of the assembly listing file. | ||||||||||||||||||||||||||||
| 47 | It is fead with each assembled opcode and generates the full | ||||||||||||||||||||||||||||
| 48 | assembly list file on the given output handle. | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | If output is undef, does not generate any output. | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =head1 EXPORTS | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | Nothing. | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =head2 new | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | my $lst = CPU::Z80::Assembler::List->new(input => $asm_input, output => \*STDOUT); | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | Creates a new object, see L<Class::Struct|Class::Struct>. | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =head2 input | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | input is the input as passed to z80asm, i.e. list of text lines to parse or iterators | ||||||||||||||||||||||||||||
| 67 | that return text lines to parse. | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =head2 output | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | output is the output file handle to receive the listing file. It can be an open | ||||||||||||||||||||||||||||
| 72 | file for writing, or one of the standard output file handles. | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | If output is undefined, no output is generated. | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =cut | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =head2 add | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | $self->add($line, $address, $bytes); | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | Adds a new opcode to the output listing. Receives the opcode L<Asm::Preproc::Line|Asm::Preproc::Line>, | ||||||||||||||||||||||||||||
| 85 | address and bytes. Generates the output lines including this new opcode. | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | The output is held in an internal buffer until an opcode for the next line is passed to a | ||||||||||||||||||||||||||||
| 88 | subsequent add() call. | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | The last output line | ||||||||||||||||||||||||||||
| 91 | is only output on flush() or DESTROY() | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =cut | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 2603 | ▶ | sub add { my($self, $opcode_line, $address, $bytes) = @_; | ||||||||||||||||||||||||||
|
Subroutine
add: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 98 | 2603 | my $output = $self->output; | |||||||||||||||||||||||||||
| 99 | 2603 | ▶ | if ($output) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 2603 | ▶ | if ($self->_current_line && $self->_current_line != $opcode_line) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->_current_line and $self->_current_line != $opcode_line
MC/DC100%2/2
$self->_current_line and $self->_current_line != $opcode_line
$self->_current_line
$self->_current_line != $opcode_line
Truth table100%3/3
A and B
A= $self->_current_lineB= $self->_current_line != $opcode_line
| |||||||||||||||||||||||||||||
| 101 | 2594 | $self->flush(); # different line | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 2603 | ▶ | if (! $self->_current_line) { # new or different line | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 2598 | $self->_current_line($opcode_line); | |||||||||||||||||||||||||||
| 106 | 2598 | $self->_current_address($address); | |||||||||||||||||||||||||||
| 107 | 2598 | $self->_current_bytes($bytes); | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | else { # same line as last | ||||||||||||||||||||||||||||
| 110 | 5 | $self->_current_bytes($self->_current_bytes . $bytes); | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =head2 flush | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | $self->flush(); | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | Dumps the current line to the output. Called on DESTROY(). | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =cut | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 2602 | ▶ | sub flush { my($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
flush: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||
| 128 | 2602 | my $output = $self->output; | |||||||||||||||||||||||||||
| 129 | 2602 | ▶ | if ($output && $self->_current_line) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$output and $self->_current_line
MC/DC50%1/2
$output and $self->_current_line
$output
$self->_current_line
Truth table66%2/3
A and B
A= $outputB= $self->_current_line
| |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | # print all input lines up to the current position | ||||||||||||||||||||||||||||
| 132 | 2598 | my $rewind_count; | |||||||||||||||||||||||||||
| 133 | my $line; | ||||||||||||||||||||||||||||
| 134 | 2598 | for (;;) { | |||||||||||||||||||||||||||
| 135 | 2608 | ▶ | while (! defined($self->_line_stream) || | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->_line_stream and defined($line = $self->_line_stream->next)
MC/DC50%1/2
defined $self->_line_stream and defined($line = $self->_line_stream->next)
defined $self->_line_stream
defined($line = $self->_line_stream->next)
Truth table66%2/3
A and B
A= defined $self->_line_streamB= defined($line = $self->_line_stream->next)
| |||||||||||||||||||||||||||||
| 136 | ! defined($line = $self->_line_stream->next)) { | ||||||||||||||||||||||||||||
| 137 | 4 | $rewind_count++; # end of input, rewind | |||||||||||||||||||||||||||
| 138 | 4 | ▶ | die "Cannot find $line in list" if $rewind_count > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | # assert input is OK | ||||||||||||||||||||||||||||
| 140 | $self->_line_stream(CPU::Z80::Assembler::z80preprocessor( | ||||||||||||||||||||||||||||
| 141 | 4 | @{$self->input})); | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 2608 | ▶ | last if $line == $self->_current_line; # found current line | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 10 | print $output $self->_format_line($self->_address, $line), "\n"; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 2598 | print $output $self->_format_line($self->_current_address, $self->_current_line); | |||||||||||||||||||||||||||
| 149 | 2598 | for (split(//, $self->_current_bytes)) { | |||||||||||||||||||||||||||
| 150 | 8778 | print $output sprintf("%02X ", ord($_)); | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | 2598 | print $output "\n"; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 2598 | $self->_address($self->_current_address); | |||||||||||||||||||||||||||
| 155 | 2598 | $self->_current_line(undef); | |||||||||||||||||||||||||||
| 156 | 2598 | $self->_current_address(undef); | |||||||||||||||||||||||||||
| 157 | 2598 | $self->_current_bytes(undef); | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 4 | ▶ | sub DESTROY { my($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 162 | 4 | $self->flush(); | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 2608 | ▶ | sub _format_line { my($self, $address, $line) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_format_line: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 168 | 2608 | ▶ | $address ||= 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$address ||= 0
MC/DC100%1/1
$address ||= 0
$address
Truth table100%2/2
A
A= $address
| |||||||||||||||||||||||||||||
| 169 | 2608 | my $text = $line->text; | |||||||||||||||||||||||||||
| 170 | 2608 | $text = expand($text); # untabify | |||||||||||||||||||||||||||
| 171 | 2608 | $text =~ s/\s+$//; | |||||||||||||||||||||||||||
| 172 | 2608 | ▶ | substr($text, 34) = ' ...' if(length($text) > 37); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 2608 | return sprintf("0x%04X: %-38s | ", $address, $text); | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||