| 1 | # | ||
| 2 | # This file is part of the Perlilog project. | ||
| 3 | # | ||
| 4 | # Copyright (C) 2003, Eli Billauer | ||
| 5 | # | ||
| 6 | # This program is free software; you can redistribute it and/or modify | ||
| 7 | # it under the terms of the GNU General Public License as published by | ||
| 8 | # the Free Software Foundation; either version 2 of the License, or | ||
| 9 | # (at your option) any later version. | ||
| 10 | # | ||
| 11 | # This program is distributed in the hope that it will be useful, | ||
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 14 | # GNU General Public License for more details. | ||
| 15 | # | ||
| 16 | # You should have received a copy of the GNU General Public License | ||
| 17 | # along with this program; if not, write to the Free Software | ||
| 18 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 19 | # | ||
| 20 | # A copy of the license can be found in a file named "licence.txt", at the | ||
| 21 | # root directory of this project. | ||
| 22 | # | ||
| 23 | |||
| 24 | # Interface object for vars and Wishbone master | ||
| 25 | sub attempt { | ||
| 26 | my $this = shift; | ||
| 27 | |||
| 28 | my @wbms = grep {ref eq 'wbm'} @_; | ||
| 29 | return undef unless ($#wbms == 0); # Single master, please | ||
| 30 | |||
| 31 | my @allvars = grep {ref eq 'vars'} @_; | ||
| 32 | return undef if ($#allvars == -1); # At least one 'vars' | ||
| 33 | |||
| 34 | return undef unless ($#_ == $#allvars+1); # Only 'vars' and one 'wbm' | ||
| 35 | |||
| 36 | my @extras = (); | ||
| 37 | my $vars = undef; | ||
| 38 | |||
| 39 | # If a vars port has a read-write-address label, it's not an "extra" | ||
| 40 | # but a port that should be mapped on the bus. Do it. | ||
| 41 | |||
| 42 | foreach (@allvars) { | ||
| 43 | my %l = $_->get('labels'); | ||
| 44 | if (grep /^(r|w|rw|wr)\d+$/, keys %l) { | ||
| 45 | return "More than one address-mapped \'vars\' port in bundle" | ||
| 46 | if (defined $vars); | ||
| 47 | $vars = $_; | ||
| 48 | } else { | ||
| 49 | push @extras, $_; | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | my $wbm = $wbms[0]; | ||
| 54 | |||
| 55 | # Now we make sure that $vars has wb_adr_bits and wb_adr_select set. | ||
| 56 | # The wishbone interfacing won't work otherwise. | ||
| 57 | |||
| 58 | my $bits = $vars->get('wb_adr_bits'); | ||
| 59 | my $select = $vars->get('wb_adr_select'); | ||
| 60 | |||
| 61 | return ("Failed to find \'wb_adr_bits\' and \'wb_adr_select\' properties in ". | ||
| 62 | $vars->who." while attempting to interface it with ".$wbm->who."\n") | ||
| 63 | unless ((defined $bits) && (defined $select)); | ||
| 64 | |||
| 65 | # Now we create ports to match with, and attempt to find some volunteer | ||
| 66 | # to do the matching. | ||
| 67 | |||
| 68 | my $self = $this->new(nick => 'vars_to_wishbone'); | ||
| 69 | |||
| 70 | # We begin with making a slave for the master port. | ||
| 71 | |||
| 72 | my $mywbs = wbs->new(nick => 'vars2wbm_wbs_port', | ||
| 73 | parent => $self); | ||
| 74 | # Here we diffuse the properties to our own slave | ||
| 75 | |||
| 76 | $mywbs->const('wb_adr_bits', $bits); | ||
| 77 | $mywbs->const('wb_adr_select', $select); | ||
| 78 | |||
| 79 | # And finally, we interface our ports. | ||
| 80 | |||
| 81 | my @objs = $self->intobjects($wbm, $mywbs, @extras); | ||
| 82 | return "Failed to match ".$self->safewho(@wbms). | ||
| 83 | " with a vars-adapting Wishbone slave\n" | ||
| 84 | unless (@objs); | ||
| 85 | |||
| 86 | return ($self, $mywbs, @objs); | ||
| 87 | } | ||
| 88 | |||
| 89 | sub generate { | ||
| 90 | my $self = shift; | ||
| 91 | |||
| 92 | # Get the ports to connect... | ||
| 93 | my @ports = $self->get('ports'); | ||
| 94 | my ($wbs) = grep {ref eq 'wbs'} @ports; | ||
| 95 | my @allvars = grep {ref eq 'vars'} $self->get('perlilog-ports-to-connect'); | ||
| 96 | |||
| 97 | # Find our target object (where we're going to write code to) | ||
| 98 | my $obj = $self->whereto; | ||
| 99 | |||
| 100 | # Now we create the necessary variables for our one slave port (the | ||
| 101 | # one connected with the master). | ||
| 102 | |||
| 103 | my %wbsNames = (); | ||
| 104 | my %wbsIDs = (); | ||
| 105 | |||
| 106 | my ($clk, $rst, $adr_i, $dat_i, $dat_o, $we_i, $stb_i, $cyc_i, $ack_o); | ||
| 107 | |||
| 108 | ($clk, $wbsIDs{'clk_i'}) = $obj->namevar('wb_clk_i', 'wire', 'in'); | ||
| 109 | ($rst, $wbsIDs{'rst_i'}) = $obj->namevar('wb_rst_i', 'wire', 'in'); | ||
| 110 | ($adr_i, $wbsIDs{'adr_i'}) = $obj->namevar('wb_adr_i', 'wire', 'in'); | ||
| 111 | ($dat_i, $wbsIDs{'dat_i'}) = $obj->namevar('wb_dat_i', 'wire', 'in'); | ||
| 112 | ($dat_o, $wbsIDs{'dat_o'}) = $obj->namevar('wb_dat_o', 'wire', 'out'); | ||
| 113 | ($we_i, $wbsIDs{'we_i'}) = $obj->namevar('wb_we_i', 'wire', 'in'); | ||
| 114 | ($stb_i, $wbsIDs{'stb_i'}) = $obj->namevar('wb_stb_i', 'wire', 'in'); | ||
| 115 | ($cyc_i, $wbsIDs{'cyc_i'}) = $obj->namevar('wb_cyc_i', 'wire', 'in'); | ||
| 116 | ($ack_o, $wbsIDs{'ack_o'}) = $obj->namevar('wb_ack_o', 'wire', 'out'); | ||
| 117 | |||
| 118 | $wbs->const('labels', %wbsIDs); # Set up the labels for the port. | ||
| 119 | |||
| 120 | $obj->append(" assign $ack_o = $cyc_i && $stb_i;\n"); | ||
| 121 | |||
| 122 | # Find our one mapped vars port. | ||
| 123 | my $vars; | ||
| 124 | my %l; | ||
| 125 | foreach (@allvars) { | ||
| 126 | %l = $_->get('labels'); | ||
| 127 | if (grep /^(r|w|rw|wr)\d+$/, keys %l) { | ||
| 128 | $vars = $_; | ||
| 129 | last; | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | # Now we set up the read and write addresses | ||
| 134 | my %reads = (); | ||
| 135 | my %writes = (); | ||
| 136 | |||
| 137 | my %lID = $self->labelID($vars); | ||
| 138 | |||
| 139 | foreach (sort keys %lID) { | ||
| 140 | if (/^(rw|wr|r)(\d+)$/) { | ||
| 141 | wrong("Double definition for read-from address ($2) on ".$vars->who."\n") | ||
| 142 | if (defined $reads{$2}); | ||
| 143 | $reads{$2} = $lID{$_}; | ||
| 144 | } | ||
| 145 | if (/^(rw|wr|w)(\d+)$/) { | ||
| 146 | wrong("Double definition for write-to address ($2) on ".$vars->who."\n") | ||
| 147 | if (defined $writes{$2}); | ||
| 148 | $writes{$2} = $lID{$_}; | ||
| 149 | } | ||
| 150 | wrong("Non-compliant label ".$self->prettyval($_). | ||
| 151 | " on bus-mapped ".$vars->who."\n") | ||
| 152 | unless (/^(rw|wr|r|w)(\d+)$/) | ||
| 153 | } | ||
| 154 | |||
| 155 | # Now to bus writes. This is a bit tricky, because we need | ||
| 156 | # a register to hold the value, and the drive the desired variable. | ||
| 157 | # Note that we do this before read cycles, because we always try to | ||
| 158 | # do attach()es before copyvar()s. (Actually, it wouldn't matter in | ||
| 159 | # this specific case, but it's a good habit). | ||
| 160 | |||
| 161 | my @writeregs = (); | ||
| 162 | my %written = (); | ||
| 163 | my ($regname, $regID); | ||
| 164 | my $writecaseclause = ''; | ||
| 165 | |||
| 166 | foreach (sort {$a <=> $b} keys %writes) { | ||
| 167 | my $wireID = $writes{$_}; | ||
| 168 | # We fecth the variable's name in the object in which it resides, | ||
| 169 | # so we can give our register a related name -- no more use for | ||
| 170 | # it. | ||
| 171 | if (defined $written{$wireID}) { | ||
| 172 | # If we have already assigned a register to the variable, just write | ||
| 173 | # on the register. This makes it possible to write to the same register | ||
| 174 | # via multiple addresses. | ||
| 175 | $regname = $written{$wireID}; | ||
| 176 | } else { | ||
| 177 | # Otherwise, we generate a special register. | ||
| 178 | my $smartname = $self->IDvar($wireID); | ||
| 179 | ($regname, $regID) = $obj->namevar($smartname.'_reg', 'reg', 'out'); | ||
| 180 | $obj->samedim($regname, $dat_i); | ||
| 181 | $written{$wireID} = $regname; | ||
| 182 | $self->attach($regID, $wireID); | ||
| 183 | push @writeregs, $regname; | ||
| 184 | } | ||
| 185 | $writecaseclause .= " $_: $regname <= #1 $dat_i;\n"; | ||
| 186 | } | ||
| 187 | |||
| 188 | my $writeclause = " always @(posedge $clk or posedge $rst)\n"; | ||
| 189 | $writeclause .= " if ($rst)\n"; | ||
| 190 | $writeclause .= " begin\n"; | ||
| 191 | foreach (@writeregs) { | ||
| 192 | $writeclause .= " $_ <= #1 0;\n"; | ||
| 193 | } | ||
| 194 | $writeclause .= " end\n"; | ||
| 195 | $writeclause .= " else if ($cyc_i && $stb_i && $we_i)\n"; | ||
| 196 | $writeclause .= " case ($adr_i)\n"; | ||
| 197 | $writeclause .= $writecaseclause; | ||
| 198 | $writeclause .= " endcase\n"; | ||
| 199 | |||
| 200 | # Now we create the code for the bus reads. | ||
| 201 | |||
| 202 | my @triggers = ($adr_i); | ||
| 203 | my %triggered = (); | ||
| 204 | |||
| 205 | my $readclause = ''; | ||
| 206 | |||
| 207 | foreach (sort {$a <=> $b} keys %reads) { | ||
| 208 | my $localname = $obj->copyvar($reads{$_}); | ||
| 209 | $readclause .= " $_: $dat_o = $localname;\n"; | ||
| 210 | $obj->samedim($dat_o, $localname); # Important! | ||
| 211 | unless (defined $triggered{$localname}) { | ||
| 212 | $triggered{$localname} = 1; | ||
| 213 | push @triggers, $localname; | ||
| 214 | } | ||
| 215 | } | ||
| 216 | |||
| 217 | $readclause .= " default: $dat_o = 0;\n"; | ||
| 218 | $readclause .= " endcase\n\n"; | ||
| 219 | |||
| 220 | # Now we put the opening "always" piece | ||
| 221 | $readclause = " always @(".join(' or ', @triggers). | ||
| 222 | ")\n case ($adr_i)\n" . $readclause; | ||
| 223 | |||
| 224 | # Finally we append our clauses. Well, only if there is some | ||
| 225 | # essence in them. | ||
| 226 | |||
| 227 | unless ($#triggers < 1) { | ||
| 228 | $obj->append($readclause) ; | ||
| 229 | # If the read clause exists, the dat_o is used as a register. | ||
| 230 | $obj->set(['vars', $dat_o, 'type'], 'reg'); | ||
| 231 | } | ||
| 232 | $obj->append($writeclause) if (@writeregs); | ||
| 233 | } | ||
| 234 | |||
| 235 | sub codetargets { | ||
| 236 | my $self = shift; | ||
| 237 | |||
| 238 | # Get the ports to connect... | ||
| 239 | my @ports = $self->get('perlilog-ports-to-connect'); | ||
| 240 | my ($wbm) = grep {ref eq 'wbm'} @ports; | ||
| 241 | my @allvars = grep {ref eq 'vars'} @ports; | ||
| 242 | |||
| 243 | # Find our one mapped vars port. | ||
| 244 | my $vars; | ||
| 245 | my %l; | ||
| 246 | foreach (@allvars) { | ||
| 247 | %l = $_->get('labels'); | ||
| 248 | if (grep /^(r|w|rw|wr)\d+$/, keys %l) { | ||
| 249 | $vars = $_; | ||
| 250 | last; | ||
| 251 | } | ||
| 252 | } | ||
| 253 | # Now we recommend these ports parents... | ||
| 254 | |||
| 255 | return ($vars->get('parent'), $wbm->get('parent')); | ||
| 256 | } |