Class: MBASICFile

Inherits:
CPMFile show all
Defined in:
lib/CPMFile.rb

Constant Summary collapse

SINGLE_BYTE_TOKENS =

the CP/M MBASIC file format is very similar to the MS-DOS BASICA/GWBASIC formats, described at www.chebucto.ns.ca/~af380/GW-BASIC-tokens.html

{
	0x81=>'END',
	0x82=>'FOR',
	0x83=>'NEXT',
	0x84=>'DATA',
	0x85=>'INPUT',
	0x86=>'DIM',
	0x87=>'READ',
	0x88=>'LET',
	0x89=>'GOTO',
	0x8A=>'RUN',
	0x8B=>'IF',
	0x8C=>'RESTORE',
	0x8D=>'GOSUB',
	0x8E=>'RETURN',
	0x8F=>'REM',
	0x90=>'STOP',
	0x91=>'PRINT',
	0x92=>'CLEAR',
	0x93=>'LIST',
	0x94=>'NEW',
	0x95=>'ON',
	0x96=>'DEF',
	0x97=>'POKE',
	0x98=>'CONT',
	0x9B=>'LPRINT',
	0x9C=>'LLIST',
	0x9D=>'WIDTH',
	0x9E=>'ELSE',
	0x9F=>'TRACE',
	0xA0=>'NOTRACE',
	0xA1=>'SWAP',
	0xA2=>'ERASE',
	0xA3=>'EDIT',
	0xA4=>'ERROR',
	0xA5=>'RESUME',
	0xA6=>'DEL',
	0xA7=>'AUTO',
	0xA8=>'RENUM',
	0xA9=>'DEFSTR',
	0xAA=>'DEFINT',
	0xAB=>'DEFSNG',
	0xAC=>'DEFDBL',
	0xAD=>'LINE',
	0xAE=>'POP',
	0xAF=>'WHILE',
	0xB0=>'WEND',
	0xB1=>'CALL',
	0xB2=>'WRITE',
	0xB3=>'COMMON',
	0xB4=>'CHAIN',
	0xB5=>'OPTION',
	0xB6=>'RANDOMIZE',
	0xB7=>'SYSTEM',
	0xB8=>'OPEN',
	0xB9=>'FIELD',
	0xBA=>'GET',
	0xBB=>'PUT',
	0xBC=>'CLOSE',
	0xBD=>'LOAD',
	0xBE=>'MERGE',
	0xBF=>'FILES',
	0xC0=>'NAME',
	0xC1=>'KILL',
	0xC2=>'LSET',
	0xC3=>'RSET',
	0xC4=>'SAVE',
	0xC5=>'RESET',
	0xC6=>'TEXT',
	0xC7=>'HOME',
	0xC8=>'VTAB',
	0xC9=>'HTAB',
	0xCA=>'INVERSE',
	0xCB=>'NORMAL',
	0xCC=>'GR',
	0xCD=>'COLOR',
	0xCE=>'HLIN',
	0xCF=>'VLIN',
	0xD0=>'PLOT',
	0xD1=>'HGR',
	0xD2=>'HPLOT',
	0xD3=>'HCOLOR',
	0xD4=>'BEEP',
	0xD5=>'WAIT',
	0xDD=>'TO',
	0xDE=>'THEN',
	0xDF=>'TAB(',
	0xE0=>'STEP',
	0xE1=>'USR',
	0xE2=>'FN',
	0xE3=>'SPC(',
	0xE4=>'NOT',
	0xE5=>'ERL',
	0xE6=>'ERR',
	0xE7=>'STRING$',
	0xE8=>'USING',
	0xE9=>'INSTR',
	0xEA=>"'",
	0xEB=>'VARPTR',
	0xEC=>'SCRN',
	0xED=>'HSCRN',
	0xEE=>'INKEY$',
	0xEF=>'>',
	0xF0=>'=',
	0xF1=>'<',
	0xF2=>'+',
	0xF3=>'-',
	0xF4=>'*',
	0xF5=>'/',
	0xF6=>'^',
	0xF7=>'AND',
	0xF8=>'OR',
	0xF9=>'XOR',
	0xFA=>'EQV',
	0xFB=>'IMP',
	0xFC=>'MOD',
	0xFD=>'\\',
}
DOUBLE_BYTE_TOKENS =
{
	0x81=>'LEFT$',
	0x82=>'RIGHT$',
	0x83=>'MID$',
	0x84=>'SGN',
	0x85=>'INT',
	0x87=>'SQR',
	0x88=>'RND',
	0x89=>'SIN',
	0x8A=>'LOG',
	0x8B=>'EXP',
	0x8C=>'COS',
	0x8D=>'TAN',
	0x8E=>'ATN',
	0x8F=>'FRE',
	0x90=>'POS',
	0x91=>'LEN',
	0x92=>'STR$',
	0x93=>'VAL',
	0x94=>'ASC',
	0x95=>'CHR$',
	0x96=>'PEEK',
	0x97=>'SPACE$',
	0x98=>'OCT$',
	0x99=>'HEX$',
	0x9A=>'LPOS',
	0x9B=>'CINT',
	0x9C=>'CSNG',
	0x9D=>'CDBL',
	0x9E=>'FIX',
	0xAA=>'CVI',
	0xAB=>'CVS',
	0xAC=>'CVD',
	0xAE=>'EOF',
	0xAF=>'LOC',
	0xB0=>'LOF',
	0xB1=>'MKI$',
	0xB2=>'MKS$',
	0xB3=>'MKD$',
	0xB4=>'VPOS',
	0xB5=>'PDL',
	0xB6=>'BUTTON',
}

Constants inherited from DSKFile

DSKFile::APPLESOFT_TOKENS

Instance Attribute Summary

Attributes inherited from CPMFile

#file_type

Attributes inherited from DSKFile

#contents, #filename

Instance Method Summary collapse

Methods inherited from CPMFile

#can_be_picture?, #file_extension, #full_filename, #initialize, split_filename, #to_png

Methods inherited from DSKFile

#==, #buffer_as_applesoft_file, #can_be_picture?, #file_extension, #hex_dump, #initialize, #length_in_sectors, #to_ascii

Constructor Details

This class inherits a constructor from CPMFile

Instance Method Details

#buffer_as_gbasic(buffer) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
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
# File 'lib/CPMFile.rb', line 240

def buffer_as_gbasic(buffer)
	s=""
	p=1
	while(p<buffer.length) && (buffer[p]!=0)
		p+=2	#skip the 2 byte pointer to the start of next line
		line_number=buffer[p]+(buffer[p+1]<<8)
		p+=2
		s+= "#{line_number} "		
		while(p<buffer.length) && (buffer[p]!=0)
			c=buffer[p]
			p+=1

			if (c==0x3A) then 	#if its a ':'

				if buffer[p]==0x9E then	#ELSE is actually stored as :ELSE
					c=buffer[p]
					p+=1
				end
				if buffer[p]==0x8F then	#' is actually stored as :REM'					
					c=buffer[p+1]
					p+=2
				end
			end
			if c<0x20 then
				case (c)
					when 0x0B	#OCTAL CONSTANT
						constant=buffer[p]+(buffer[p+1]<<8)
						s+=sprintf("&O%o",constant)
						p+=2

					when 0x0C	#HEX CONSTANT
						constant=buffer[p]+(buffer[p+1]<<8)
						s+=sprintf("&H%X",constant)
						p+=2
					when 0x0E	#DECIMAL LINE NUMBER
						constant=buffer[p]+(buffer[p+1]<<8)
						s+=sprintf("%d",constant)
						p+=2
					when 0x0F	#one byte constant
						constant=buffer[p]
						s+=sprintf("%d",constant)
						p+=1
					when 0x11..0x1B	#packed constant 0..9
						s+=sprintf("%d",c-0x11)
					when 0x1C	#DECIMAL LINE NUMBER
						constant=buffer[p]+(buffer[p+1]<<8)
						s+=sprintf("%d",constant)
						p+=2
          when 0x1D #4 byte floating point
          	constant=parse_bytes_as_float(buffer[p..p+3].reverse)
            s+=constant.to_s
            p+=4
          when 0x1F #8 byte floating point
          	constant=parse_bytes_as_float(buffer[p..p+7].reverse)
            s+=constant.to_s
            p+=8
				end

			elsif c<0x80 then
				s+=c.chr
			else

				if c==0xFF then
					
					c=buffer[p]
					p+=1
					token=DOUBLE_BYTE_TOKENS[c]
				else
					token=SINGLE_BYTE_TOKENS[c]
				end
				s+=(token.nil?) ? sprintf("[%02X]",c):token

			end
		end
		s+="\n"
	p+=1
	end
	s	
end

#parse_bytes_as_float(bytes) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/CPMFile.rb', line 223

def parse_bytes_as_float(bytes)
	return 0.0 if bytes[0]==0
	exponent=2**(bytes[0]-0x80)
	first_byte=bytes[1]
	sign=-1.0	#assume it's negative
	if first_byte<0x80 then
		sign=1.0
		first_byte+=0x80
	end	
	mantissa=(first_byte/256.0)*sign
	2.upto(bytes.length-1) do |i|
		mantissa+=(bytes[i]/(256.0**i))
	end
	return sprintf("%8f",exponent*mantissa).to_f
end

#to_sObject



215
216
217
218
219
220
221
# File 'lib/CPMFile.rb', line 215

def to_s
  if @contents[0]==0xFF then
    buffer_as_gbasic(@contents)
  else
    @contents
  end
end