Constraints.pm 8.93 KB
Newer Older
1
2
3
#-----------------------------------------------------------------------
# Fault InJection Instrumenter (FIJI)
# https://embsys.technikum-wien.at/projects/vecs/fiji
4
#
5
6
# Copyright (C) 2017 Christian Fibich <fibich@technikum-wien.at>
# Copyright (C) 2017 Stefan Tauner <tauner@technikum-wien.at>
7
#
8
9
# This module is free software; you can redistribute it and/or modify
# it under the same terms as Perl itself.
10
#
11
12
13
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
#
15
16
# See the LICENSE file for more details.
#-----------------------------------------------------------------------
17

Christian Fibich's avatar
Christian Fibich committed
18
19
20
21
22
23
## @file Constraints.pm
# @brief Contains class \ref FIJI::Constraints

## @class FIJI::Constraints
# @brief Contains functions to generate Constraints for Synthesis/P&R Tools

24
25
26
27
28
package FIJI::Constraints;

use strict;
use warnings;

29
use Log::Log4perl qw(get_logger);
30

31
32
use FIJI qw(:fiji_version);

Christian Fibich's avatar
Christian Fibich committed
33
sub _quartus_partitions_logiclock {
34
    my $logger = get_logger("");
Christian Fibich's avatar
Christian Fibich committed
35
    my $cfg    = shift;
36

37
38
    my $DUT_TO  = $cfg->{'dut_module'} . ":" . $cfg->{'dut_inst'};
    my $FIJI_TO = $cfg->{'fiji_module'} . ":" . $cfg->{'fiji_inst'};
39

40
41
    my $DUT_PART_NAME  = "PARTITION_" . $cfg->{'dut_module'};
    my $FIJI_PART_NAME = "PARTITION_" . $cfg->{'fiji_module'};
Christian Fibich's avatar
Christian Fibich committed
42
43
    my $DUT_COLOR      = (sprintf("%d", 0xFF0000));
    my $FIJI_COLOR     = (sprintf("%d", 0x00FF00));
44
45

    my $time = localtime;
46
47
    my $fiji_version = FIJI_VERSION;
    my $design_id = sprintf("0x%04x", $cfg->{'id'});
48
49
50
51

    my $txt = <<"END_HDR";
# ------------------------------------------------------------------------------
# FIJI Physical Placement Constraints File for Quartus II
52
53
54
55
#
# Generated at $time
# by FIJI $fiji_version
# for Design ID $design_id
56
57
58
59
# ------------------------------------------------------------------------------
END_HDR
    my $fin = "# EOF";

60
    my $partitioning = <<"END_PARTITION";
61
62
63
64
65
66
67
68
69
70
set_global_assignment -name PARTITION_NETLIST_TYPE POST_SYNTH -section_id $DUT_PART_NAME
set_global_assignment -name PARTITION_COLOR $DUT_COLOR -section_id $DUT_PART_NAME
set_global_assignment -name PARTITION_NETLIST_TYPE POST_SYNTH -section_id $FIJI_PART_NAME
set_global_assignment -name PARTITION_COLOR $FIJI_COLOR -section_id $FIJI_PART_NAME
set_instance_assignment -name PARTITION_HIERARCHY root_partition -to | -section_id Top
set_instance_assignment -name PARTITION_HIERARCHY db/dut1 -to $DUT_TO -section_id $DUT_PART_NAME
set_instance_assignment -name PARTITION_HIERARCHY db/fiji1 -to $FIJI_TO -section_id $FIJI_PART_NAME

END_PARTITION

71
    my $area = <<"END_LOCK";
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
set_global_assignment -name LL_ENABLED ON -section_id $DUT_PART_NAME
set_global_assignment -name LL_AUTO_SIZE ON -section_id $DUT_PART_NAME
set_global_assignment -name LL_STATE FLOATING -section_id $DUT_PART_NAME
set_global_assignment -name LL_WIDTH 1 -section_id $DUT_PART_NAME
set_global_assignment -name LL_HEIGHT 1 -section_id $DUT_PART_NAME
set_global_assignment -name LL_ORIGIN X1_Y1 -section_id $DUT_PART_NAME
set_instance_assignment -name LL_MEMBER_OF $DUT_PART_NAME -to $DUT_TO -section_id $DUT_PART_NAME

set_global_assignment -name LL_ENABLED ON -section_id $FIJI_PART_NAME
set_global_assignment -name LL_AUTO_SIZE ON -section_id $FIJI_PART_NAME
set_global_assignment -name LL_STATE FLOATING -section_id $FIJI_PART_NAME
set_global_assignment -name LL_WIDTH 1 -section_id $FIJI_PART_NAME
set_global_assignment -name LL_HEIGHT 1 -section_id $FIJI_PART_NAME
set_global_assignment -name LL_ORIGIN X1_Y1 -section_id $FIJI_PART_NAME
set_instance_assignment -name LL_MEMBER_OF $FIJI_PART_NAME -to $FIJI_TO -section_id $FIJI_PART_NAME
END_LOCK

89
    if ($cfg->{'mode'} eq "OPT_OFF") {
90
        $txt .= $partitioning;
Christian Fibich's avatar
Christian Fibich committed
91
    } elsif ($cfg->{'mode'} eq "FIX_PLACEMENT") {
92
        $txt .= $partitioning . $area;
Christian Fibich's avatar
Christian Fibich committed
93
    } elsif ($cfg->{'mode'} ne "ALLOW") {
Christian Fibich's avatar
Christian Fibich committed
94
        $logger->error("Unknown SYNTHESIS_MODE " . $cfg->{'mode'});
95
        return undef;
96
    }
Christian Fibich's avatar
Christian Fibich committed
97
    return ($txt . $fin , 'quartus.qsf');
98
99
}

Christian Fibich's avatar
Christian Fibich committed
100
sub _synplify_optimizations {
101
    my $logger = get_logger("");
Christian Fibich's avatar
Christian Fibich committed
102
    my $cfg    = shift;
103
104
105
106
107

    my $DUT_NAME  = $cfg->{'dut_module'};
    my $FIJI_NAME = $cfg->{'fiji_module'};

    my $time = localtime;
108
109
    my $fiji_version = FIJI_VERSION;
    my $design_id = sprintf("0x%04x", $cfg->{'id'});
110
111
112
113

    my $txt = <<"END_HDR";
# ------------------------------------------------------------------------------
# FIJI Constraints File for Synplify
114
115
116
117
#
# Generated at $time
# by FIJI $fiji_version
# for Design ID $design_id
118
119
120
121
# ------------------------------------------------------------------------------
END_HDR
    my $fin = "# EOF";

Christian Fibich's avatar
Christian Fibich committed
122
    my $no_optimization = <<"END_OPTIMIZATION";
123
###==== BEGIN Attributes 
Christian Fibich's avatar
Christian Fibich committed
124
125
126
127
128
129
define_compile_point {v:work.$DUT_NAME} -type {locked}
define_compile_point {v:work.$FIJI_NAME} -type {locked}
define_attribute {v:work.$DUT_NAME}  syn_allow_retiming {0}
define_attribute {v:work.$DUT_NAME}  syn_sharing {0}
define_attribute {v:work.$DUT_NAME}  syn_pipeline {0}
define_attribute {v:work.$DUT_NAME}  syn_netlist_hierarchy {1}
130
###==== END Attributes 
131
END_OPTIMIZATION
132

133
    if ($cfg->{'mode'} eq "OPT_OFF" || $cfg->{'mode'} eq "FIX_PLACEMENT") {
134
        $txt .= $no_optimization;
Christian Fibich's avatar
Christian Fibich committed
135
    } elsif ($cfg->{'mode'} ne "ALLOW") {
Christian Fibich's avatar
Christian Fibich committed
136
        $logger->error("Unknown SYNTHESIS_MODE " . $cfg->{'mode'});
137
        return undef;
138
    }
Christian Fibich's avatar
Christian Fibich committed
139
    return ($txt . $fin, 'synplify.fdc');
140
141
}

Christian Fibich's avatar
Christian Fibich committed
142
sub _vivado_pblocks {
143
    my $logger = get_logger("");
Christian Fibich's avatar
Christian Fibich committed
144
    my $cfg    = shift;
145

Christian Fibich's avatar
Christian Fibich committed
146
147
148
    my $DUT_NAME    = $cfg->{'dut_inst'};
    my $FIJI_NAME   = $cfg->{'fiji_gen'}.'.'.$cfg->{'fiji_inst'};
    my $FIJI_PBLOCK = $cfg->{'fiji_gen'}.'_'.$cfg->{'fiji_inst'};
149

150

Christian Fibich's avatar
Christian Fibich committed
151
    my $time      = localtime;
152
153
    my $fiji_version = FIJI_VERSION;
    my $design_id = sprintf("0x%04x", $cfg->{'id'});
154
155
    my $txt = <<"END_HDR";
# ------------------------------------------------------------------------------
Christian Fibich's avatar
Christian Fibich committed
156
# FIJI Constraints File for Vivado
157
158
159
160
#
# Generated at $time
# by FIJI $fiji_version
# for Design ID $design_id
161
162
163
# ------------------------------------------------------------------------------
END_HDR
    my $fin = "# EOF";
164
165
166
167
168
169
170
171
172
173
174
    my $no_opt = <<"END_NO_OPT";
# Turn off cross-boundary optimizations

# do not even look at the original netlist
set_property dont_touch TRUE [get_cells -quiet [list $DUT_NAME]]

# do not optimize accross FIJI boundaries
set_property keep_hierarchy TRUE [get_cells -quiet [list $FIJI_NAME]]

END_NO_OPT

Christian Fibich's avatar
Christian Fibich committed
175
    my $area = <<"END_AREA";
176
177
178

# Create physical constraints for FIJI and DUI

Christian Fibich's avatar
Christian Fibich committed
179
180
181
182
183
create_pblock pblock_$DUT_NAME
create_pblock pblock_$FIJI_PBLOCK
add_cells_to_pblock [get_pblocks pblock_$DUT_NAME] [get_cells -quiet [list $DUT_NAME]]
add_cells_to_pblock [get_pblocks pblock_$FIJI_PBLOCK] [get_cells -quiet [list $FIJI_NAME]]

184
185
186
187
188
# Set size and location of the PBlocks. This default size will likely fail.
# You will likely need to resize them in the GUI.
resize_pblock [get_pblocks pblock_$DUT_NAME] -add {SLICE_X1Y1:SLICE_X10Y10}
resize_pblock [get_pblocks pblock_$FIJI_PBLOCK] -add {SLICE_X11Y1:SLICE_X15Y3}
END_AREA
Christian Fibich's avatar
Christian Fibich committed
189
    if ($cfg->{'mode'} eq "FIX_PLACEMENT") {
190
        $txt .= $no_opt;
Christian Fibich's avatar
Christian Fibich committed
191
        $txt .=  $area;
192
193
    } elsif ($cfg->{'mode'} eq "OPT_OFF") {
        $txt .= $no_opt;
194
    } elsif ($cfg->{'mode'} ne "ALLOW" && $cfg->{'mode'} ne "OPT_OFF") {
Christian Fibich's avatar
Christian Fibich committed
195
        $logger->error("Unknown SYNTHESIS_MODE " . $cfg->{'mode'});
196
        return undef;
197
    }
Christian Fibich's avatar
Christian Fibich committed
198
    return ($txt . $fin, 'vivado.xdc');
199
200
}

Christian Fibich's avatar
Christian Fibich committed
201
## @var %$toolhash stores which sub to execute for which tool
Christian Fibich's avatar
Christian Fibich committed
202
my $syn_toolhash = {
203
    SYNPLIFY_PRO => {
Christian Fibich's avatar
Christian Fibich committed
204
205
        ALTERA_QUARTUS => \&_synplify_optimizations,
        XILINX_VIVADO  => \&_synplify_optimizations,
206
207
208
    },
};

Christian Fibich's avatar
Christian Fibich committed
209
210
211
212
213
214
my $pr_toolhash = {
    ALTERA_QUARTUS => \&_quartus_partitions_logiclock,
    XILINX_VIVADO  => \&_vivado_pblocks,
};

sub generate_constraints {
215
    my $logger = get_logger("");
Christian Fibich's avatar
Christian Fibich committed
216
    my ($class, $synthesis_tool, $pr_tool, $cfg, $basename) = @_;
217

Christian Fibich's avatar
Christian Fibich committed
218
219
220
221
    my ($file,$txt,$postfix);

    my $synhash = $syn_toolhash->{$synthesis_tool};
    my $prhash  = $pr_toolhash->{$pr_tool};
222

Christian Fibich's avatar
Christian Fibich committed
223
224
    return "No entries for synthesis tool found" if (!defined $synhash);
    return "No entries for P&R tool found" if (!defined $prhash);
225
226

    my $sub = $synhash->{$pr_tool};
227

Christian Fibich's avatar
Christian Fibich committed
228
    return "No subentries for P&R tool in Synthesis toolhash found" if (!defined $sub);
229

Christian Fibich's avatar
Christian Fibich committed
230
    ($txt, $postfix) = &{$sub}($cfg);
231
    return "Could not generate constraints for $synthesis_tool/$pr_tool." if (!defined $txt);
232

Christian Fibich's avatar
Christian Fibich committed
233
234
235
236
237
238
239
240
241
242
243
244
    my $filename = $basename.'.'.$postfix;

    open($file, ">", $filename) or return "Cannot open file $filename: $!";
    print $file $txt;
    close($file) or return "Cannot close file $filename: $!";
    $logger->info("Successfully generated synthesis constraints for $synthesis_tool/$pr_tool.");

    ($txt, $postfix) = &{$prhash}($cfg);
    return "Could not generate P&R constraints for $pr_tool." if (!defined $txt);
    $filename = $basename.'.'.$postfix;

    open($file, ">", $filename) or return "Cannot open file $filename: $!";
245
    print $file $txt;
Christian Fibich's avatar
Christian Fibich committed
246
247
    close($file) or return "Cannot close file $filename: $!";
    $logger->info("Successfully generated P&R constraints for $pr_tool.");
248
249
250
251
252

    return undef;
}

1;