File Coverage

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


line stmt bran cond sub pod time code
1             package VIC::PIC::Functions::CodeGen;
2 31     31   20433 use strict;
  31         47  
  31         871  
3 31     31   114 use warnings;
  31         40  
  31         1533  
4             our $VERSION = '0.29';
5             $VERSION = eval $VERSION;
6 31     31   113 use Carp;
  31         36  
  31         1774  
7 31     31   541 use POSIX ();
  31         4732  
  31         633  
8 31     31   112 use Moo::Role;
  31         45  
  31         200  
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 1070 my ($self, $var) = @_;
43 227 50       445 return undef unless defined $var;
44 227 100       810 return 0 if $var =~ /^\d+$/;
45 216 50       919 return 0 unless $self->doesrole('Chip');
46 216 100       1376 return 1 if exists $self->pins->{$var};
47 75 100       517 return 1 if exists $self->registers->{$var};
48 30 50 33     79 return 1 if ($self->doesrole('Timer', 1) and exists $self->timer_pins->{$var});
49 30 100 33     73 return 1 if ($self->doesrole('Timer', 1) and exists $self->wdt_prescaler->{$var});
50 29 100 33     67 return 1 if ($self->doesrole('USART', 1) and exists $self->usart_pins->{$var});
51 9         33 return 0;
52             }
53              
54             sub validate_operator {
55 133     133 0 493 my ($self, $op) = @_;
56 133 50       514 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       378 return lc $vop if defined $vop;
64             }
65              
66             sub validate_modifier_operator {
67 16     16 0 92 my ($self, $mod) = @_;
68 16 100       93 my $vmod = "op_$mod" if $mod =~ /^
69             SQRT | HIGH | LOW
70             /x;
71 16 100       76 return lc $vmod if defined $vmod;
72             }
73              
74             sub update_code_config {
75 20     20 0 105 my ($self, $grp, $key, $val) = @_;
76 20 50       160 return unless $self->doesrole('CodeGen');
77 20 50       57 return unless defined $grp;
78 20         35 $grp = lc $grp; # force lower case in case of usage for things like SPI/UART/I2C
79 20 100       94 $self->code_config->{$grp} = {} unless exists $self->code_config->{$grp};
80 20         40 my $grpref = $self->code_config->{$grp};
81 20 100       46 if ($key eq 'bits') {
82 2 50       5 $val = 8 unless defined $val;
83 2 50       6 $val = 8 if $val <= 8;
84 2 50 33     7 $val = 16 if ($val > 8 and $val <= 16);
85 2 50 33     6 $val = 32 if ($val > 16 and $val <= 32);
86 2 50       4 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       54 $val = 1 unless defined $val;
91 20 50       52 if (ref $grpref eq 'HASH') {
92 20         38 $grpref->{$key} = $val;
93             } else {
94 0         0 $self->code_config->{$grp} = { $key => $val };
95             }
96 20         44 1;
97             }
98              
99             sub address_bits {
100 257     257 0 1483 my ($self, $varname) = @_;
101 257 50       481 return unless $self->doesrole('CodeGen');
102 257         575 my $bits = $self->code_config->{variable}->{bits};
103 257 50       416 return $bits unless $varname;
104 257   33     1015 $bits = $self->code_config->{lc $varname}->{bits} || $bits;
105 257         562 return $bits;
106             }
107              
108             sub is_variable {
109 5     5 0 10 my ($self, $varname) = @_;
110 5 50       11 return unless $varname;
111 5 50       12 return unless $self->doesrole('CodeGen');
112 5 100       36 return 1 if defined $self->code_config->{lc $varname};
113 1         3 return 0;
114             }
115              
116              
117             1;
118             __END__