| 1 | package VIC::Command; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Getopt::Long; | ||
| 6 | use VIC; | ||
| 7 | |||
| 8 | our $VERSION = '0.29'; | ||
| 9 | $VERSION = eval $VERSION; | ||
| 10 | |||
| 11 | sub usage { | ||
| 12 | my $usage = << '...'; | ||
| 13 | Usage: vic [options] <input file> | ||
| 14 | |||
| 15 | -h, --help This help message | ||
| 16 | --version Version number | ||
| 17 | --verbose Verbose printing during compilation | ||
| 18 | -p, --pic <PIC> Use this PIC choice instead of the one in the code | ||
| 19 | -o, --output <file> Writes the compiled syntax to the given output file | ||
| 20 | -d, --debug Dump the compile tree for debugging | ||
| 21 | -i, --intermediate Inline the intermediate code with the output | ||
| 22 | --simulate Simulate the code that was compiled | ||
| 23 | --list-chips List the supported microcontroller chips | ||
| 24 | --list-simulators List the supported simulators | ||
| 25 | --check <PIC> Checks if the given PIC is supported | ||
| 26 | --list-features <PIC> Lists the features of the PIC | ||
| 27 | --chip-pinout <PIC> Draws the pin diagram of the PIC on screen | ||
| 28 | --no-hex Does not generate the hex file using gputils | ||
| 29 | --list-gputils List the path for the gputils executables | ||
| 30 | |||
| 31 | ... | ||
| 32 | die $usage; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub version { | ||
| 36 | my $txt = << "..."; | ||
| 37 | VIC version $VERSION | ||
| 38 | ... | ||
| 39 | die $txt; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub print_chips { | ||
| 43 | my @chips = VIC::supported_chips(); | ||
| 44 | my $ctxt = join("\n", @chips); | ||
| 45 | my $txt = << "..."; | ||
| 46 | VIC supports the following microcontroller chips: | ||
| 47 | $ctxt | ||
| 48 | |||
| 49 | ... | ||
| 50 | print $txt; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub print_sims { | ||
| 54 | my @sims = VIC::supported_simulators(); | ||
| 55 | my $stxt = join("\n", @sims); | ||
| 56 | my $txt = << "..."; | ||
| 57 | VIC supports the following simulators: | ||
| 58 | $stxt | ||
| 59 | |||
| 60 | ... | ||
| 61 | print $txt; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub list_gputils { | ||
| 65 | my @utils = VIC::gputils(); | ||
| 66 | if (@utils) { | ||
| 67 | print "gpasm: $utils[0]\ngplink: $utils[1]\n"; | ||
| 68 | } else { | ||
| 69 | print "No gputils found."; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | sub check_support { | ||
| 74 | my $chip = shift; | ||
| 75 | my $flag = VIC::is_chip_supported($chip); | ||
| 76 | die "VIC does not support '$chip'\n" unless $flag; | ||
| 77 | unless (@_) { | ||
| 78 | print "VIC supports '$chip'\n" if $flag; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | sub list_features { | ||
| 83 | my $chip = shift; | ||
| 84 | check_support($chip); | ||
| 85 | my $hh = VIC::list_chip_features($chip); | ||
| 86 | if ($hh) { | ||
| 87 | my $rtxt = join("\n", @{$hh->{roles}}); | ||
| 88 | print "\n$chip supports the following features:\n$rtxt\n"; | ||
| 89 | print "\n$chip has the following memory capabilities:\n"; | ||
| 90 | my $mh = $hh->{memory}; | ||
| 91 | foreach (keys %$mh) { | ||
| 92 | my $units = 'bytes'; | ||
| 93 | $units = 'words' if $_ =~ /flash/i; # HACK | ||
| 94 | print "$_: " . $mh->{$_} . " $units\n"; | ||
| 95 | } | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | sub chip_pinouts { | ||
| 100 | my $chip = shift; | ||
| 101 | check_support($chip, 1); | ||
| 102 | VIC::print_pinout($chip); | ||
| 103 | } | ||
| 104 | |||
| 105 | sub run { | ||
| 106 | my ($class, @args) = @_; | ||
| 107 | local @ARGV = @args; | ||
| 108 | |||
| 109 | my $debug = 0; | ||
| 110 | my $output = ''; | ||
| 111 | my $help = 0; | ||
| 112 | my $pic = undef; | ||
| 113 | my $intermediate = undef; | ||
| 114 | my $version = 0; | ||
| 115 | my $verbose = 0; | ||
| 116 | my $list_chips = 0; | ||
| 117 | my $list_sims = 0; | ||
| 118 | my $check_support = undef; | ||
| 119 | my $chip_features = undef; | ||
| 120 | my $chip_pinouts = undef; | ||
| 121 | my $no_hex = 0; | ||
| 122 | my $list_gputils = 0; | ||
| 123 | my $simulate = 0; | ||
| 124 | |||
| 125 | GetOptions( | ||
| 126 | "output=s" => \$output, | ||
| 127 | "debug" => \$debug, | ||
| 128 | "help" => \$help, | ||
| 129 | "pic=s" => \$pic, | ||
| 130 | "intermediate" => \$intermediate, | ||
| 131 | "version" => \$version, | ||
| 132 | "verbose" => \$verbose, | ||
| 133 | "list-chips" => \$list_chips, | ||
| 134 | "list-simulators" => \$list_sims, | ||
| 135 | "list-features=s" => \$chip_features, | ||
| 136 | "chip-pinout=s" => \$chip_pinouts, | ||
| 137 | "check=s" => \$check_support, | ||
| 138 | "no-hex" => \$no_hex, | ||
| 139 | "list-gputils" => \$list_gputils, | ||
| 140 | "simulate" => \$simulate, | ||
| 141 | ) or usage(); | ||
| 142 | usage() if $help; | ||
| 143 | version() if $version; | ||
| 144 | print_chips() if $list_chips; | ||
| 145 | print_sims() if $list_sims; | ||
| 146 | check_support($check_support) if $check_support; | ||
| 147 | list_features($chip_features) if $chip_features; | ||
| 148 | chip_pinouts($chip_pinouts) if $chip_pinouts; | ||
| 149 | list_gputils() if $list_gputils; | ||
| 150 | return if ($list_chips or $list_sims or $check_support or | ||
| 151 | $chip_features or $list_gputils or $chip_pinouts); | ||
| 152 | |||
| 153 | $VIC::Debug = $debug; | ||
| 154 | $VIC::Intermediate = $intermediate; | ||
| 155 | $VIC::Verbose = $verbose; | ||
| 156 | |||
| 157 | my $fh; | ||
| 158 | if (length $output) { | ||
| 159 | $no_hex = 1 if $output =~ /\.asm$/; | ||
| 160 | $output =~ s/\.hex$/\.asm/g; | ||
| 161 | open $fh, ">$output" or die "Unable to open $output: $!"; | ||
| 162 | } else { | ||
| 163 | $fh = *STDOUT; | ||
| 164 | $no_hex = 1; # no file name given so print to screen | ||
| 165 | } | ||
| 166 | return usage() unless scalar @ARGV; | ||
| 167 | if (defined $pic) { | ||
| 168 | $pic =~ s/^PIC/P/gi; | ||
| 169 | $pic = lc $pic; | ||
| 170 | } | ||
| 171 | my ($compiled_out, $chip, $sim) = VIC::compile(do {local $/; <>}, $pic); | ||
| 172 | print $fh $compiled_out; | ||
| 173 | warn "Cannot simulate unless -o/--output option is used." if ($simulate and $no_hex); | ||
| 174 | return if $no_hex; | ||
| 175 | my $ret = VIC::assemble($chip, $output) if length $output; | ||
| 176 | return $ret unless $simulate; | ||
| 177 | return VIC::simulate($sim, $ret); | ||
| 178 | } | ||
| 179 | |||
| 180 | 1; | ||
| 181 | |||
| 182 | =encoding utf8 | ||
| 183 | |||
| 184 | =head1 NAME | ||
| 185 | |||
| 186 | VIC::Command | ||
| 187 | |||
| 188 | =head1 SYNOPSIS | ||
| 189 | |||
| 190 | The command-line tool for compiling VIC files. | ||
| 191 | |||
| 192 | =head1 DESCRIPTION | ||
| 193 | |||
| 194 | To view all the options run | ||
| 195 | $ vic -h | ||
| 196 | |||
| 197 | |||
| 198 | =head1 AUTHOR | ||
| 199 | |||
| 200 | Vikas N Kumar <vikas@cpan.org> | ||
| 201 | |||
| 202 | =head1 COPYRIGHT | ||
| 203 | |||
| 204 | Copyright (c) 2014. Vikas N Kumar | ||
| 205 | |||
| 206 | This program is free software; you can redistribute it and/or modify it | ||
| 207 | under the same terms as Perl itself. | ||
| 208 | |||
| 209 | See http://www.perl.com/perl/misc/Artistic.html | ||
| 210 | |||
| 211 | =cut |