Class: AtariBasic

Inherits:
NativeFileType show all
Defined in:
lib/native_file_types/atari800/AtariBasic.rb

Constant Summary collapse

START_OF_FILE_PART_TWO =
14
ATARI_BASIC_COMMAND_TOKENS =
[
'REM', #0x0
'DATA', #0x1
'INPUT', #0x2
'COLOR', #0x3
'LIST', #0x4
'ENTER', #0x5
'LET', #0x6
'IF', #0x7
'FOR', #0x8
'NEXT', #0x9
'GOTO', #0x0A
'GO TO', #0x0B
'GOSUB', #0x0C
'TRAP', #0x0D
'BYE', #0x0E
'CONT', #0x0F
'COM', #0x10
'CLOSE', #0x11
'CLR', #0x12
'DEG', #0x13
'DIM', #0x14
'END', #0x15
'NEW', #0x16
'OPEN', #0x17
'LOAD', #0x18
'SAVE', #0x19
'STATUS', #0x1A
'NOTE', #0x1B
'POINT', #0x1C
'XIO', #0x1D
'ON', #0x1E
'POKE', #0x1F
'PRINT', #0x20
'RAD', #0x21
'READ', #0x22
'RESTORE', #0x23
'RETURN', #0x24
'RUN', #0x25
'STOP', #0x26
'POP', #0x27
'?', #0x28
'GET', #0x29
'PUT', #0x2A
'GRAPHICS', #0x2B
'PLOT', #0x2C
'POSITION', #0x2D
'DOS', #0x2E
'DRAWTO', #0x2F
'SETCOLOR', #0x30
'LOCATE', #0x31
'SOUND', #0x32
'LPRINT', #0x33
'CSAVE', #0x34
'CLOAD', #0x35
'', #0x36 - [IMPLIED LET]
'ERROR - [SYNTAX]', #0x37

## these following tokens are from Turbo BASIC - http://www.atarimax.com/jindroush.atari.org/afmtbas1.html
'DPOKE', #38
'MOVE', #39
'-MOVE', #3A
'*F', #3B
'REPEAT', #3C
'UNTIL', #3D
'WHILE', #3E
'WEND', #3F
'ELSE', #40
'ENDIF', #41
'BPUT', #42
'BGET', #43
'FILLTO', #44
'DO', #45
'LOOP', #46
'EXIT', #47
'DIR', #48
'LOCK', #49
'UNLOCK', #4A
'RENAME', #4B
'DELETE', #4C
'PAUSE', #4D
'TIME$=', #4E
'PROC', #4F
'EXEC', #50
'ENDPROC', #51
'FCOLOR', #52
'*L', #53
'------------------------------', #54
'RENUM', #55
'DEL', #56
'DUMP', #57
'TRACE', #58
'TEXT', #59
'BLOAD', #5A
'BRUN', #5B
'GO#', #5C
'#', #5D
'*B', #5E
'PAINT', #5F
'CLS', #60
'DSOUND', #61
'CIRCLE', #62
'%PUT', #63
'%GET', #64

]
ATARI_BASIC_OPERATORS_AND_FUNCTIONS =
{
0x0D => '[NUM HEX CONST]', # from TURBO BASIC
0x0E =>'[NUM CONST]',
0x0F =>'[STR CONST]',
0x10 =>'[NOT USED]',
0x11 =>'[NOT USED]',
0x12 =>',',
0x13 =>'$',
0x14 =>':',# [STMT END]
0x15 =>';',
0x16 =>'[LINE END]',
0x17 =>'GOTO',
0x18 =>'GOSUB',
0x19 =>'TO',
0x1A =>'STEP',
0x1B =>'THEN',
0x1C =>'#',
0x1D =>'<=', #numerics
0x1E =>'<>',
0x1F =>'>=',
0x20 =>'<',
0x21 =>'>',
0x22 =>'=',
0x23 =>'',
0x24 =>'*',
0x25 =>'+',
0x26 =>'-',
0x27 =>'/',
0x28 =>'NOT',
0x29 =>'OR',
0x2A =>'AND',
0x2B =>'(',
0x2C =>')',
0x2D =>'=', # ARITHM ASSIGN
0x2E =>'=',#STRING ASSIGN
0x2F =>'<=',#STRINGS
0x30 =>'<>',
0x31 =>'>=',
0x32 =>'<',
0x33 =>'>',
0x34 =>'=',
0x35 =>'+',#unary
0x36 =>'-',
0x37 =>'(',# [STRING LEFT PAREN]
0x38 =>'(',#[ARRAY LEFT PAREN]
0x39 =>'(',# [DIM ARRAY LEFT PAREN]
0x3A =>'(',#[FUN LEFT PAREN]
0x3B =>'(',# [DIM STR LEFT PAREN]
0x3C =>',',# [ARRAY COMMA]
0x3D =>'STR$',
0x3E =>'CHR$',
0x3F =>'USR',
0x40 =>'ASC',
0x41 =>'VAL',
0x42 =>'LEN',
0x43 =>'ADR',
0x44 =>'ATN',
0x45 =>'COS',
0x46 =>'PEEK',
0x47 =>'SIN',
0x48 =>'RND',
0x49 =>'FRE',
0x4A =>'EXP',
0x4B =>'LOG',
0x4C =>'CLOG',
0x4D =>'SQR',
0x4E =>'SGN',
0x4F =>'ABS',
0x50 =>'INT',
0x51 =>'PADDLE',
0x52 =>'STICK',
0x53 =>'PTRIG',
0x54 =>'STRIG',

## these following tokens are from Turbo BASIC - http://www.atarimax.com/jindroush.atari.org/afmtbas1.html
0x55=>'DPEEK',
0x56=>'&',
0x57=>'!',
0x58=>'INSTR',
0x59=>'INKEY$',
0x5A=>'EXOR',
0x5B=>'HEX$',
0x5C=>'DEC',
0x5D=>'DIV',
0x5E=>'FRAC',
0x5F=>'TIME$',
0x60=>'TIME',
0x61=>'MOD',
0x62=>'EXEC',
0x63=>'RND',
0x64=>'RAND',
0x65=>'TRUNC',
0x66=>'%0',
0x67=>'%1',
0x68=>'%2',
0x69=>'%3',
0x6A=>'GO#',
0x6B=>'UINSTR',
0x6C=>'ERR',
0x6D=>'ERL',
}

Instance Attribute Summary

Attributes inherited from NativeFileType

#aux_code, #contents, #file_system_image, #file_type, #filename, #meta_data

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from NativeFileType

#<=>, #==, all_native_file_types, best_fit, code_for_tests, compatability_score, #data_without_header, file_type_matches?, #full_filename, #header_length, #initialize, is_valid_file_if, #load_address, load_address, matching_score, native_file_types_possible_on_file_system, non_matching_score, #to_hex_dump, #to_info_dump, #type_description

Methods included from SubclassTracking

extended

Constructor Details

This class inherits a constructor from NativeFileType

Class Method Details

.file_system_file_typesObject



33
34
35
36
37
# File 'lib/native_file_types/atari800/AtariBasic.rb', line 33

def self.file_system_file_types
  {
    AtariDos=>:any
  }
end

Instance Method Details

#to_listingObject



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/native_file_types/atari800/AtariBasic.rb', line 252

def to_listing
  s=""
  variable_name_table_displacement=(contents[2]+(contents[3]<<8))-0x100
#  puts "%02x %02x" % [contents[6],contents[7]]
  variable_value_table_displacement=(contents[6]+(contents[7]<<8))-0x100
  statement_table_displacement=(contents[8]+(contents[9]<<8))-0x100
  file_end_displacement=(contents[12]+(contents[13]<<8))-0x100
  
#  puts "DVNT $#{"%02x" % variable_name_table_displacement} DVVT $#{"%02x" % variable_value_table_displacement} DST $#{"%02x" % statement_table_displacement}"
  
  #extract all the variable names
  variable_name_table=contents[variable_name_table_displacement+START_OF_FILE_PART_TWO,(variable_value_table_displacement-variable_name_table_displacement)]
  variable_names={}
  variable_token=0x80
  current_variable_name=""
  variable_name_table.each_byte do |byte|
    if byte>=0x80 then      
      current_variable_name+=(byte-0x80).chr
      variable_names[variable_token]=current_variable_name
      variable_token+=1
      current_variable_name=""
    else
      current_variable_name+=byte.chr
    end
  end
  #~ variable_names.keys.sort.each do |token|
    #~ puts "$%02x : #{variable_names[token]}" % token
  #~ end
  
  statement_table=contents[statement_table_displacement+START_OF_FILE_PART_TWO,(file_end_displacement-statement_table_displacement)]
  p=0
  s=""
  while p<statement_table.length    
#    puts "%02x" % statement_table[p] 
    line_number=statement_table[p,2].unpack("v")[0]
    break if line_number==0x8000
    line_length=statement_table[p+2]
    line=statement_table[p+3,line_length-2]
    s<<"#{line_number} "
#    puts "#{line_number} #{"%02x" % line_length}"
    break if line_length==0
    t=0 
    start_of_statement=true
    while (!line.nil?) && t<line.length && line[t]!=0x16 
      actual_file_offset=statement_table_displacement+START_OF_FILE_PART_TWO+p+t #track for debugging
      token=line[t]      
      if start_of_statement then
        t+=1 #skip over the 'displacement till next statement' byte
        token=line[t]
        command=ATARI_BASIC_COMMAND_TOKENS[token]
        command="[UNK CMD %02X]" % token if command.nil?
        s<<"#{command} " unless token==0x36 #don't print anything on an implied let
        start_of_statement=false
        if token==0x00 then #REM
          s<<line[t+1,line.length-(t+3)]
          t=line.length
        end
      elsif token>=0x80 then
        variable=variable_names[token]
        variable="[UNK VAR %02X]" % token if variable.nil?
        s<<variable
      elsif token==0x14 then #end of statement
        s<<":"
        start_of_statement=true
      elsif token==0x0D then #numeric hex constant - TURBO BASIC
#        raise "NHCONST not implemented yet : offset $#{"%x" % actual_file_offset} of #{filename}"
        const=line[t+1]+(line[t+2]<<8)
        s<<"&%x"%const
        t+=2
      elsif token==0x0E then #numeric constant - 6 BCD floating point
        buffer=line[t+1,6]
        s<<parse_bytes_as_float(buffer).to_s.sub(/\.0$/,'')#strip off ".0"
        t+=6
      elsif token==0x0F then #string
        length=line[t+1]        
        s<<"\"#{line[t+2,length]}\""
        t+=length+1
      elsif token==0x1B then
        s<<"THEN "
        start_of_statement=true        
      else
        op_func=ATARI_BASIC_OPERATORS_AND_FUNCTIONS[token]
        op_func="[UNK OP/FUNC %02X]" % token if op_func.nil?
        s<<"#{op_func}"        
      end      
        
      t+=1
    end
    s<<"\n"
    p+=line_length
  end
  s
end