#!/usr/bin/ruby require 'stringio' src_file = ARGV.shift sio = StringIO::new def_table = {} open(src_file).each{|line| line.chomp! case line when /#define +([^ ]+) +([^ ]+)/ def_table[$1] = $2 when /SFR *\(([^,]+), *([^)]+)\); +\/\/ +/i, /SFR +([^ ]+) *= *(0x[0-9a-f]+) *; *\/\* +/i name= $1 addr = $2 suffix = $' def_table[name] = addr str = "__sfr __at (#{addr}) #{name}" line = sprintf("%-32s; /* %-40s */", str, suffix.gsub(/ *\*\//, '')) if name == 'EMI0CN' then # For sdcc xram access str = line = [line, sprintf("%-32s; /* %-40s */", "__sfr __at (#{addr}) _XPAGE", "@see 4.1.1 pdata access by SFR")] end when /SFR16 *\(([^,]+), *([^)]+)\); +\/\/ +/i, /SFR16 +([^ ]+) *= *(0x[0-9a-f]+) *; *\/\* +/i name= $1 addr = $2 suffix = $' addr = addr.to_i(0) addr = "0x#{(addr + 1).to_s(16)}#{addr.to_s(16)}".gsub(/[a-f]/){|c| c.upcase} str = "__sfr16 __at (#{addr}) #{name}" line = sprintf("%-32s; /* %-40s */", str, suffix.gsub(/ *\*\//, '')) when /SBIT *\(([^,]+), *([^,]+), *([^)]+)\); +\/\/ +/i, /SBIT +([^ ]+) *= *([^^]+)\^([0-7]) *; *\/\* +/i name= $1 addr = "#{$2} + #{$3}" suffix = $' if def_table[$2] then addr = "0x#{(def_table[$2].to_i(0) + $3.to_i).to_s(16)}".gsub(/[a-f]/){|c| c.upcase} end str = "__sbit __at (#{addr}) #{name}" line = sprintf("%-32s; /* %-40s */", str, suffix.gsub(/ *\*\//, '')) end if line.kind_of?(Array) line.each{|v| sio.puts v} else sio.puts line end } sio.seek(0) $stdout.puts sio.read