Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Revive bin/rcpu-as

  • Loading branch information...
commit 60e375e68c5942b4705c87b5e2f98149c9faa77e 1 parent f8e6614
Magnus Holm authored

Showing 2 changed files with 68 additions and 5 deletions. Show diff stats Hide diff stats

  1. +8 5 bin/rcpu
  2. +60 0 bin/rcpu-as
13 bin/rcpu
@@ -11,19 +11,22 @@ p = T::Parser.new do
11 11 DCPU-16 assembler, emulator, debugger
12 12
13 13 Usage:
14   - bin/rcpu examples/hello.rcpu
15   - bin/rcpu examples/hello.bin
16   - bin/rcpu examples/hello.s
17   - bin/rcpu -b examples/hello
  14 + bin/rcpu [options] file
18 15
19 16 Valid extensions:
20 17 .rcpu - Ruby DSL assembler
21 18 .s - Notch-style assembler
22 19 .bin - Binary file
23 20
  21 +Examples:
  22 + bin/rcpu examples/hello.rcpu
  23 + bin/rcpu examples/hello.bin
  24 + bin/rcpu examples/hello.s
  25 + bin/rcpu -b examples/hello
  26 +
24 27 Available options:
25 28 EOS
26   - opt :binary, "Always treat the file as a binary"
  29 + opt :binary, "Ignore the extension and treat the file as a binary"
27 30 end
28 31
29 32 formats = %w[bin rcpu]
60 bin/rcpu-as
... ... @@ -0,0 +1,60 @@
  1 +#!/usr/bin/env ruby
  2 +$LOAD_PATH.unshift(File.expand_path("../../lib", __FILE__))
  3 +require 'rcpu'
  4 +require 'rcpu/trollop'
  5 +T = RCPU::Trollop
  6 +
  7 +EXTENSIONS = {
  8 + 'binary' => 'bin',
  9 + 'hex' => 'hex',
  10 +}
  11 +
  12 +opts = T.options do
  13 + version "RCPU #{RCPU::VERSION}"
  14 + banner <<-EOS
  15 +rcpu-as - a DCPU-16 assembler
  16 +
  17 +Usage:
  18 + rcpu-as [options] file
  19 +
  20 +Valid file extensions:
  21 + .rcpu - Ruby DSL assembler
  22 + .s - Notch-style assembler
  23 +
  24 +Available options:
  25 +EOS
  26 +
  27 + opt :output, "Output filename", :short => 'o', :type => :string
  28 + opt :format, "Output format (#{EXTENSIONS.keys*'|'})", :short => 'O', :default => 'binary'
  29 + opt :version, "Print version and exit"
  30 + opt :help, "Show this message", :short => 'h'
  31 +
  32 + text <<-EOS
  33 +
  34 +If no output filename is given the output will be written to filename.(#{EXTENSIONS.values*'|'}).
  35 + EOS
  36 +end
  37 +
  38 +T.die "invalid output format #{opts[:format].inspect}" unless EXTENSIONS.member?(opts[:format])
  39 +T.die "filename required" unless input_filename = ARGV.shift
  40 +
  41 +if opts[:output]
  42 + output_io = File.open(opts[:output], 'w')
  43 +else
  44 + filename = File.join(File.dirname(input_filename),
  45 + File.basename(input_filename, '.rcpu') + '.' + EXTENSIONS[opts[:format]])
  46 + output_io = File.open(filename, 'w')
  47 +end
  48 +
  49 +linker = RCPU::Linker.new
  50 +linker.compile(input_filename, Dir.pwd)
  51 +data = linker.finalize
  52 +
  53 +case opts.format
  54 +when 'binary'
  55 + output_io.write data.pack('v*')
  56 +when 'hex'
  57 + data.each do |x|
  58 + printf "%04x\n", x
  59 + end
  60 +end

0 comments on commit 60e375e

Please sign in to comment.
Something went wrong with that request. Please try again.