Class: C64Asm::Data

Inherits:
Nop
  • Object
show all
Defined in:
lib/c64asm/asm.rb

Overview

Data is a bunch of bytes

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Nop

#label, #ready?, #to_a

Constructor Details

#initialize(data, mode = :default) ⇒ Data

Create new data nop Handles a couple of input modes.



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/c64asm/asm.rb', line 334

def initialize(data, mode = :default)
  case data
  when String
    raise DataError, 'Unimplemented mode' unless [:default, :screen].member? mode
    @length = data.length
  when Array
    raise DataError, 'Unimplemented mode' unless [:default, :word].member? mode
    @length = mode == :word ? 2 * data.length : data.length
  end

  @data = data
  @mode = mode

  validate
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



330
331
332
# File 'lib/c64asm/asm.rb', line 330

def data
  @data
end

#lengthObject (readonly)

Returns the value of attribute length.



330
331
332
# File 'lib/c64asm/asm.rb', line 330

def length
  @length
end

#modeObject (readonly)

Returns the value of attribute mode.



330
331
332
# File 'lib/c64asm/asm.rb', line 330

def mode
  @mode
end

Instance Method Details

#to_binaryObject

Turn data into a byte string



391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/c64asm/asm.rb', line 391

def to_binary
  case @data
  when String
    case @mode
    when :default
      @data.each_codepoint.to_a.collect{|p| PETSCII[p]}.pack('C*')
    when :screen
      @data.upcase.each_codepoint.to_a.collect{|p| CHAR_MAP[p]}.pack('C*')
    end
  when Array
    case @mode
    when :default
      @data.pack('C*')
    when :word
      @data.collect{|e| [e.ls_byte, e.ms_byte]}.flatten.pack('C*')
    end
  end
end

#to_sObject

Return pretty string representation



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/c64asm/asm.rb', line 351

def to_s
  string = '<Data: '

  case @data
  when String
    string += '"' + (@data.length > 16 ? @data.slice(0, 16) + '...' : @data) + '"'
  when Array
    slice = @data.length > 8 ? @data.slice(0, 8) : @data
    string += slice.collect{|e| '$' + e.to_s(16)}.join(',')
    string += '...' if slice.length != @data.length
  end

  if @mode != :default
    string += " (#{mode})"
  end

  string += '>'
end

#to_sourceObject

Return source code representation



371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/c64asm/asm.rb', line 371

def to_source
  case @data
  when String
    case @mode
    when :default
      ".text \"#{@data}\""
    when :screen
      ".screen \"#{@data}\""
    end
  when Array
    case @mode
    when :default
      ".byte #{@data.collect{|e| '$' + e.to_s(16)}.join(',')}"
    when :word
      ".word #{@data.collect{|e| '$' + e.to_s(16)}.join(',')}"
    end
  end
end

#validateObject (private)

Validate data



412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/c64asm/asm.rb', line 412

def validate
  case @data
  when String
    case @mode
    when :default
      @data.each_codepoint{|p| raise DataError, 'Invalid data' unless PETSCII.has_key? p}
    when :screen
      @data.upcase.each_codepoint{|p| raise DataError, 'Invalid data' unless CHAR_MAP.has_key? p}
    end
  when Array
    case @mode
    when :default
      @data.each{|e| raise DataError, 'Invalid data' unless (e >= 0 and e <= 255)}
    when :word
      @data.each{|e| raise DataError, 'Invalid data' unless (e >= 0 and e <= 65535)}
    end
  end
end