Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Revive bin/rcpu-as

  • Loading branch information...
commit 60e375e68c5942b4705c87b5e2f98149c9faa77e 1 parent f8e6614
@judofyr authored
Showing with 68 additions and 5 deletions.
  1. +8 −5 bin/rcpu
  2. +60 −0 bin/rcpu-as
View
13 bin/rcpu
@@ -11,19 +11,22 @@ p = T::Parser.new do
DCPU-16 assembler, emulator, debugger
Usage:
- bin/rcpu examples/hello.rcpu
- bin/rcpu examples/hello.bin
- bin/rcpu examples/hello.s
- bin/rcpu -b examples/hello
+ bin/rcpu [options] file
Valid extensions:
.rcpu - Ruby DSL assembler
.s - Notch-style assembler
.bin - Binary file
+Examples:
+ bin/rcpu examples/hello.rcpu
+ bin/rcpu examples/hello.bin
+ bin/rcpu examples/hello.s
+ bin/rcpu -b examples/hello
+
Available options:
EOS
- opt :binary, "Always treat the file as a binary"
+ opt :binary, "Ignore the extension and treat the file as a binary"
end
formats = %w[bin rcpu]
View
60 bin/rcpu-as
@@ -0,0 +1,60 @@
+#!/usr/bin/env ruby
+$LOAD_PATH.unshift(File.expand_path("../../lib", __FILE__))
+require 'rcpu'
+require 'rcpu/trollop'
+T = RCPU::Trollop
+
+EXTENSIONS = {
+ 'binary' => 'bin',
+ 'hex' => 'hex',
+}
+
+opts = T.options do
+ version "RCPU #{RCPU::VERSION}"
+ banner <<-EOS
+rcpu-as - a DCPU-16 assembler
+
+Usage:
+ rcpu-as [options] file
+
+Valid file extensions:
+ .rcpu - Ruby DSL assembler
+ .s - Notch-style assembler
+
+Available options:
+EOS
+
+ opt :output, "Output filename", :short => 'o', :type => :string
+ opt :format, "Output format (#{EXTENSIONS.keys*'|'})", :short => 'O', :default => 'binary'
+ opt :version, "Print version and exit"
+ opt :help, "Show this message", :short => 'h'
+
+ text <<-EOS
+
+If no output filename is given the output will be written to filename.(#{EXTENSIONS.values*'|'}).
+ EOS
+end
+
+T.die "invalid output format #{opts[:format].inspect}" unless EXTENSIONS.member?(opts[:format])
+T.die "filename required" unless input_filename = ARGV.shift
+
+if opts[:output]
+ output_io = File.open(opts[:output], 'w')
+else
+ filename = File.join(File.dirname(input_filename),
+ File.basename(input_filename, '.rcpu') + '.' + EXTENSIONS[opts[:format]])
+ output_io = File.open(filename, 'w')
+end
+
+linker = RCPU::Linker.new
+linker.compile(input_filename, Dir.pwd)
+data = linker.finalize
+
+case opts.format
+when 'binary'
+ output_io.write data.pack('v*')
+when 'hex'
+ data.each do |x|
+ printf "%04x\n", x
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.