/[VRac]/Tape.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /Tape.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 192 - (show annotations)
Sat Apr 12 14:20:01 2008 UTC (16 years ago) by dpavlin
File size: 2092 byte(s)
remove debugging info
1 package Tape;
2
3 # Dobrica Pavlinusic, <dpavlin@rot13.org> 08/03/07 11:11:56 CEST
4
5 use strict;
6 use warnings;
7
8 use base qw/Class::Accessor/;
9 __PACKAGE__->mk_accessors(qw(tape tape_path tape_pos trace));
10
11 use File::Slurp;
12 use Carp qw/confess/;
13
14 =head1 NAME
15
16 Tape - implement tape reader/recorder
17
18 =cut
19
20 =head2 read_tape
21
22 my $byte = $self->read_tape;
23
24 =cut
25
26 my $last_warn = '';
27
28 sub _warn {
29 my $msg = shift;
30 if ( $msg ne $last_warn ) {
31 warn "$msg\n";
32 $last_warn = $msg;
33 }
34 }
35
36 my $c_0 = 0;
37 my $c_1 = 0;
38 my $mask = 1;
39
40 sub read_tape {
41 my $self = shift;
42 if ( ! $self->tape ) {
43 _warn "please load tape!";
44 return 0;
45 }
46 my $pos = $self->tape_pos;
47 my $tape = $self->tape;
48 if ( $pos > length( $tape ) ) {
49 _warn "end of tape [$pos]";
50 return -1;
51 }
52
53 if ( $c_0 ) {
54 $c_0--;
55 return 0;
56 }
57 if ( $c_1 ) {
58 $c_1--;
59 return 255;
60 }
61
62 $mask = $mask << 1;
63 if ( $mask > 0x80 ) {
64 $pos++;
65 $self->tape_pos( $pos );
66 $mask = 1;
67 };
68
69 my $byte = ord( substr($self->tape,$pos,1) );
70 my $bit = $byte & $mask;
71 # warn sprintf("## tape pos %d 0x%04x mask %02x and %02x = %d\n", $pos, $pos, $mask, $byte, $bit); # if $self->trace;
72
73 ( $c_0, $c_1 ) = ( 0x17, 0x17 );
74 ( $c_0, $c_1 ) = ( 0x30, 0x30 ) if $bit;
75
76 return 0;
77 }
78
79 =head2 write_tape
80
81 $self->write_tape( $byte );
82
83 =cut
84
85
86 sub write_tape {
87 my ( $self, $byte ) = @_;
88
89 $self->append_to_file( 'tape.dmp', $byte );
90
91 return $byte;
92 }
93
94 =head2 load_tape
95
96 $self->load_tape( '/path/to/file' );
97
98 =cut
99
100 sub load_tape {
101 my $self = shift;
102 my $path = shift || return;
103
104 my $tape = read_file( $path ) || confess "can't load $path: $!";
105 $self->tape_path( $path );
106
107 $self->tape_pos( 0 );
108 $self->tape( $tape );
109 warn "loaded tape $path ", -s $path, " bytes\n";
110 return 1;
111 }
112
113 =head2 tape_status
114
115 print $self->tape_status;
116
117 =cut
118
119 sub tape_status {
120 my $self = shift;
121
122 return "No tape in (simulated) drive" unless $self->tape;
123
124 my $size = length( $self->tape );
125
126 return sprintf(
127 "tape file: %s with %d 0x%x bytes, current position: %d 0x%x",
128 $self->tape_path, $size, $size, $self->pos, $self->pos,
129 );
130 }
131
132 =head1 SEE ALSO
133
134 L<VRac>
135
136 =cut
137
138 1;

  ViewVC Help
Powered by ViewVC 1.1.26