| 1 | package VIC::PIC::Functions::CodeGen; | ||||||||||||||||||||||||||||
| 2 | 31 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 31 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 31 | our $VERSION = '0.32'; | |||||||||||||||||||||||||||
| 5 | 31 | $VERSION = eval $VERSION; | |||||||||||||||||||||||||||
| 6 | 31 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 31 | ▶ | use POSIX (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 31 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | # default | ||||||||||||||||||||||||||||
| 11 | 31 | has org => (is => 'ro', default => 0); | |||||||||||||||||||||||||||
| 12 | 51 | ▶ | has code_config => (is => 'rw', default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | { | ||||||||||||||||||||||||||||
| 14 | 51 | 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 | 31 | }); | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 227 | ▶ | sub validate { | ||||||||||||||||||||||||||
|
Subroutine
validate: called CC=12 SCAR=25.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 42 | 227 | my ($self, $var) = @_; | |||||||||||||||||||||||||||
| 43 | 227 | ▶ | return undef unless defined $var; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 227 | ▶ | return 0 if $var =~ /^\d+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 216 | ▶ | return 0 unless $self->doesrole('Chip'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 216 | ▶ | return 1 if exists $self->pins->{$var}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 75 | ▶ | return 1 if exists $self->registers->{$var}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 30 | ▶ | return 1 if ($self->doesrole('Timer', 1) and exists $self->timer_pins->{$var}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->doesrole("Timer", 1) and exists $self->timer_pins->{$var}
MC/DC0%0/2
$self->doesrole("Timer", 1) and exists $self->timer_pins->{$var}
$self->doesrole("Timer", 1)
exists $self->timer_pins->{$var}
Truth table33%1/3
A and B
A= $self->doesrole("Timer", 1)B= exists $self->timer_pins->{$var}
| |||||||||||||||||||||||||||||
| 49 | 30 | ▶ | return 1 if ($self->doesrole('Timer', 1) and exists $self->wdt_prescaler->{$var}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->doesrole("Timer", 1) and exists $self->wdt_prescaler->{$var}
MC/DC50%1/2
$self->doesrole("Timer", 1) and exists $self->wdt_prescaler->{$var}
$self->doesrole("Timer", 1)
exists $self->wdt_prescaler->{$var}
Truth table66%2/3
A and B
A= $self->doesrole("Timer", 1)B= exists $self->wdt_prescaler->{$var}
| |||||||||||||||||||||||||||||
| 50 | 29 | ▶ | return 1 if ($self->doesrole('USART', 1) and exists $self->usart_pins->{$var}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->doesrole("USART", 1) and exists $self->usart_pins->{$var}
MC/DC50%1/2
$self->doesrole("USART", 1) and exists $self->usart_pins->{$var}
$self->doesrole("USART", 1)
exists $self->usart_pins->{$var}
Truth table66%2/3
A and B
A= $self->doesrole("USART", 1)B= exists $self->usart_pins->{$var}
| |||||||||||||||||||||||||||||
| 51 | 9 | return 0; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 133 | ▶ | sub validate_operator { | ||||||||||||||||||||||||||
|
Subroutine
validate_operator: called CC=3 SCAR=11.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 55 | 133 | my ($self, $op) = @_; | |||||||||||||||||||||||||||
| 56 | 133 | ▶ | my $vop = "op_$op" if $op =~ /^ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | ▶ | return lc $vop if defined $vop; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 16 | ▶ | sub validate_modifier_operator { | ||||||||||||||||||||||||||
|
Subroutine
validate_modifier_operator: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 67 | 16 | my ($self, $mod) = @_; | |||||||||||||||||||||||||||
| 68 | 16 | ▶ | my $vmod = "op_$mod" if $mod =~ /^ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | SQRT | HIGH | LOW | ||||||||||||||||||||||||||||
| 70 | /x; | ||||||||||||||||||||||||||||
| 71 | 16 | ▶ | return lc $vmod if defined $vmod; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 20 | ▶ | sub update_code_config { | ||||||||||||||||||||||||||
|
Subroutine
update_code_config: called CC=15 SCAR=30.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 75 | 20 | my ($self, $grp, $key, $val) = @_; | |||||||||||||||||||||||||||
| 76 | 20 | ▶ | return unless $self->doesrole('CodeGen'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 20 | ▶ | return unless defined $grp; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 20 | $grp = lc $grp; # force lower case in case of usage for things like SPI/UART/I2C | |||||||||||||||||||||||||||
| 79 | 20 | ▶ | $self->code_config->{$grp} = {} unless exists $self->code_config->{$grp}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 20 | my $grpref = $self->code_config->{$grp}; | |||||||||||||||||||||||||||
| 81 | 20 | ▶ | if ($key eq 'bits') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 2 | ▶ | $val = 8 unless defined $val; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 2 | ▶ | $val = 8 if $val <= 8; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 2 | ▶ | $val = 16 if ($val > 8 and $val <= 16); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$val > 8 and $val <= 16
MC/DC0%0/2
$val > 8 and $val <= 16
$val > 8
$val <= 16
Truth table33%1/3
A and B
A= $val > 8B= $val <= 16
| |||||||||||||||||||||||||||||
| 85 | 2 | ▶ | $val = 32 if ($val > 16 and $val <= 32); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$val > 16 and $val <= 32
MC/DC0%0/2
$val > 16 and $val <= 32
$val > 16
$val <= 32
Truth table33%1/3
A and B
A= $val > 16B= $val <= 32
| |||||||||||||||||||||||||||||
| 86 | 2 | ▶ | carp "$val-bits is not supported. Maximum supported size is 64-bit" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | if $val > 64; | ||||||||||||||||||||||||||||
| 88 | 2 | ▶ | $val = 64 if $val > 32; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | 20 | ▶ | $val = 1 unless defined $val; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 20 | ▶ | if (ref $grpref eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 20 | $grpref->{$key} = $val; | |||||||||||||||||||||||||||
| 93 | } else { | ||||||||||||||||||||||||||||
| 94 | 0 | $self->code_config->{$grp} = { $key => $val }; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | 20 | 1; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 257 | ▶ | sub address_bits { | ||||||||||||||||||||||||||
|
Subroutine
address_bits: called CC=4 SCAR=15.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 100 | 257 | my ($self, $varname) = @_; | |||||||||||||||||||||||||||
| 101 | 257 | ▶ | return unless $self->doesrole('CodeGen'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 257 | my $bits = $self->code_config->{variable}->{bits}; | |||||||||||||||||||||||||||
| 103 | 257 | ▶ | return $bits unless $varname; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 257 | ▶ | $bits = $self->code_config->{lc $varname}->{bits} || $bits; | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->code_config->{lc $varname}{'bits'} || $bits
MC/DC0%0/2
$self->code_config->{lc $varname}{'bits'} || $bits
$self->code_config->{lc $varname}{'bits'}
$bits
Truth table33%1/3
A || B
A= $self->code_config->{lc $varname}{'bits'}B= $bits
| |||||||||||||||||||||||||||||
| 105 | 257 | return $bits; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 5 | ▶ | sub is_variable { | ||||||||||||||||||||||||||
|
Subroutine
is_variable: called CC=4 SCAR=14.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 109 | 5 | my ($self, $varname) = @_; | |||||||||||||||||||||||||||
| 110 | 5 | ▶ | return unless $varname; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 5 | ▶ | return unless $self->doesrole('CodeGen'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 5 | ▶ | return 1 if defined $self->code_config->{lc $varname}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 1 | return 0; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 31 | 1; | |||||||||||||||||||||||||||
| 118 | __END__ | ||||||||||||||||||||||||||||