Class: C64Asm::Data
Overview
Data is a bunch of bytes
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#length ⇒ Object
readonly
Returns the value of attribute length.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
Instance Method Summary collapse
-
#initialize(data, mode = :default) ⇒ Data
constructor
Create new data nop Handles a couple of input modes.
-
#to_binary ⇒ Object
Turn data into a byte string.
-
#to_s ⇒ Object
Return pretty string representation.
-
#to_source ⇒ Object
Return source code representation.
-
#validate ⇒ Object
private
Validate data.
Methods inherited from Nop
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
#data ⇒ Object (readonly)
Returns the value of attribute data.
330 331 332 |
# File 'lib/c64asm/asm.rb', line 330 def data @data end |
#length ⇒ Object (readonly)
Returns the value of attribute length.
330 331 332 |
# File 'lib/c64asm/asm.rb', line 330 def length @length end |
#mode ⇒ Object (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_binary ⇒ Object
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_s ⇒ Object
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_source ⇒ Object
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 |
#validate ⇒ Object (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 |