File Coverage

blib/lib/VIC/PIC/Functions/CodeGen.pm
Criterion Covered Total %
statement 59 60 98.3
branch 40 58 68.9
condition 8 18 44.4
subroutine 11 11 100.0
pod 0 6 0.0
total 118 153 77.1


line stmt bran cond sub pod time code
1             package VIC::PIC::Functions::CodeGen;
2 31     31   19329 use strict;
  31         61  
  31         816  
3 31     31   138 use warnings;
  31         49  
  31         1403  
4             our $VERSION = '0.32';
5             $VERSION = eval $VERSION;
6 31     31   140 use Carp;
  31         56  
  31         1689  
7 31     31   231 use POSIX ();
  31         100  
  31         577  
8 31     31   139 use Moo::Role;
  31         57  
  31         203  
9              
10             # default
11             has org => (is => 'ro', default => 0);
12             has code_config => (is => 'rw', default => sub {
13             {
14             debounce => {
15             count => 5,
16             delay => 1000, # in microseconds
17             },
18             adc => {
19             right_justify => 1,
20             vref => 0,
21             internal => 0,
22             },
23             variable => {
24             bits => 8, # bits. same as register_size
25             export => 0, # do not export variables
26             },
27             string => {
28             size => 32, # character allocation of null strings
29             },
30             uart => {
31             baud => 9600, # baud rate
32             bit9 => 0, # allow 9 bits
33             },
34             usart => {
35             baud => 9600, # baud rate
36             bit9 => 0, # allow 9 bits
37             },
38             }
39             });
40              
41             sub validate {
42 227     227 0 1347 my ($self, $var) = @_;
43 227 50       492 return undef unless defined $var;
44 227 100       888 return 0 if $var =~ /^\d+$/;
45 216 50       820 return 0 unless $self->doesrole('Chip');
46 216 100       1252 return 1 if exists $self->pins->{$var};
47 75 100       405 return 1 if exists $self->registers->{$var};
48 30 50 33     72 return 1 if ($self->doesrole('Timer', 1) and exists $self->timer_pins->{$var});
49 30 100 66     83 return 1 if ($self->doesrole('Timer', 1) and exists $self->wdt_prescaler->{$var});
50 29 100 66     66 return 1 if ($self->doesrole('USART', 1) and exists $self->usart_pins->{$var});
51 9         37 return 0;
52             }
53              
54             sub validate_operator {
55 133     133 0 614 my ($self, $op) = @_;
56 133 50       600 my $vop = "op_$op" if $op =~ /^
57             LE | GE | GT | LT | EQ | NE |
58             ADD | SUB | MUL | DIV | MOD |
59             BXOR | BOR | BAND | AND | OR | SHL | SHR |
60             ASSIGN | INC | DEC | NOT | COMP |
61             TBLIDX | ARRIDX | STRIDX
62             /x;
63 133 50       424 return lc $vop if defined $vop;
64             }
65              
66             sub validate_modifier_operator {
67 16     16 0 128 my ($self, $mod) = @_;
68 16 100       119 my $vmod = "op_$mod" if $mod =~ /^
69             SQRT | HIGH | LOW
70             /x;
71 16 100       70 return lc $vmod if defined $vmod;
72             }
73              
74             sub update_code_config {
75 20     20 0 136 my ($self, $grp, $key, $val) = @_;
76 20 50       135 return unless $self->doesrole('CodeGen');
77 20 50       56 return unless defined $grp;
78 20         40 $grp = lc $grp; # force lower case in case of usage for things like SPI/UART/I2C
79 20 100       161 $self->code_config->{$grp} = {} unless exists $self->code_config->{$grp};
80 20         264 my $grpref = $self->code_config->{$grp};
81 20 100       111 if ($key eq 'bits') {
82 2 50       9 $val = 8 unless defined $val;
83 2 50       7 $val = 8 if $val <= 8;
84 2 50 33     7 $val = 16 if ($val > 8 and $val <= 16);
85 2 50 33     5 $val = 32 if ($val > 16 and $val <= 32);
86 2 50       5 carp "$val-bits is not supported. Maximum supported size is 64-bit"
87             if $val > 64;
88 2 50       4 $val = 64 if $val > 32;
89             }
90 20 100       73 $val = 1 unless defined $val;
91 20 50       76 if (ref $grpref eq 'HASH') {
92 20         46 $grpref->{$key} = $val;
93             } else {
94 0         0 $self->code_config->{$grp} = { $key => $val };
95             }
96 20         53 1;
97             }
98              
99             sub address_bits {
100 257     257 0 2020 my ($self, $varname) = @_;
101 257 50       549 return unless $self->doesrole('CodeGen');
102 257         633 my $bits = $self->code_config->{variable}->{bits};
103 257 50       419 return $bits unless $varname;
104 257   33     970 $bits = $self->code_config->{lc $varname}->{bits} || $bits;
105 257         680 return $bits;
106             }
107              
108             sub is_variable {
109 5     5 0 13 my ($self, $varname) = @_;
110 5 50       15 return unless $varname;
111 5 50       23 return unless $self->doesrole('CodeGen');
112 5 100       49 return 1 if defined $self->code_config->{lc $varname};
113 1         2 return 0;
114             }
115              
116              
117             1;
118             __END__