File size: 6,782 Bytes
ae4627d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
"use strict";

//----------------------------------------------------------------------
//-- Boards
//----------------------------------------------------------------------
//-- CONFIGURATION FILES
//-----------------------------------------------------------------------
//-- The Boards are described by three .json files, located in the folder  
//-- resources/boards/boardname
//--    * pinout.json:  Pin numbers, names and type (input, output, inout)
//--    * info.json: Board information: resources, board name, interface...
//--    * rules.json: Automatic connection of unused pins
//-- 
//-- In addition there are two more OPTIONAL files:
//--    * pinout.pcf (optional): Board constraints. This is just for documentation
//--      purposes (the actual constraint file is automatically generated on 
//--      the fly when the circuit is being sinthesized)
//--    * pinout.svg (optional): Drawing of all the pins
//----------------------------------------------------------------------------
//-- DATA STRUCTURES
//----------------------------------------------------------------------------
//-- ICESTUDIO converts the .json files into objects. You can access to all   
//-- the boars from the following GLOBAL OBJECT:
//--   
//--  common.boards
//--     * name:  Board name. Ex. alhmabra_ii
//--     * info:  Resources
//--     * pinout: Pin names, number and type
//--     * rules:  If the pin is automatically connected if not used
//--     * type:   FPGA familty and type


//---------------------------------------------------------------
//-- MENU menu.json
//-- This file contains the names of all the available boards
//-- Only the boards located in that file are READ and inserted  
//-- into the common.boards GLOBAL object
//---------------------------------------------------------------

angular
  .module("icestudio")
  .service("boards", function (utils, common, nodeFs, nodePath) {

    //-- Default board
    const DEFAULT = "alhambra-ii";

    //-----------------------------------------------------------------
    //-- Read all the boards FILES and store all the information  
    //-- in the GLOBAL OBJECT: common.boards
    //-----------------------------------------------------------------
    //-- Only the boards located in the menu.json FILE are READ
    //-----------------------------------------------------------------
    this.loadBoards = function () {

      let boards = [];

      //-- Construct the Boards path: "resources/boards"
      let path = nodePath.join("resources", "boards");

      //-- Read the board menu json file and convert into an object
      let menu = nodeFs.readFileSync(nodePath.join(path, "menu.json"));
      menu = JSON.parse(menu);
      
      //-- The menu is divided in big sections: The FPGA family: ICE40HX8k, ICE40HX4k, 
      //-- ICE40LPHX, UP5K, ECP5... 
      menu.forEach(FPGAfamily => {

        //-- Access to all the boards from the current family
        FPGAfamily.boards.forEach(function (boardname) {

          //-- The data from every board is located in the  
          //-- folder "resources/boards/<boardname>"
          let boardPath = nodePath.join(path, boardname);

          //-- Every board should have at least their three MANDATORY files:
          //-- info.json, pinout.json and rules.
          //--------------------------------------------------------------------
          //-- TODO: It can be improved: It is better to distinguis between
          //--  among different errores, instead only one. If anyone introduces  
          //--  a bad board, it is difficult to find where is the error....
          //--------------------------------------------------------------------
          try {
            if (
              nodeFs.statSync(boardPath).isDirectory() &&
              nodeFs.statSync(nodePath.join(boardPath, "info.json")).isFile() &&
              nodeFs.statSync(nodePath.join(boardPath, "pinout.json")).isFile() &&
              nodeFs.statSync(nodePath.join(boardPath, "rules.json")).isFile()
            ) {

              //-- Board files ok. READ them!!
              let info = readJSONFile(boardPath, "info.json");
              let pinout = readJSONFile(boardPath, "pinout.json");
              let rules = readJSONFile(boardPath, "rules.json");

              //-- Fill the boards structure will all the information
              //-- obtained from the files
              boards.push({
                name: boardname, 
                info: info,      //-- Board resources
                pinout: pinout,  //-- Board pins
                rules: rules,    //-- Board rules
                type: FPGAfamily.type,  //-- FPGA family type
              });
            }

          //-- There was an error reading the board files
          } catch (error) {
            console.error("Board not well configured", error.message);
          }
        });
      });

      //-- The boards are available through the GLOBAL OBJECT common.boards
      common.boards = boards;
    };


    //---- PENDING: DOCUMENTATION!!!!!!

    function readJSONFile(filepath, filename) {
      var ret = {};
      try {
        var data = nodeFs.readFileSync(nodePath.join(filepath, filename));
        ret = JSON.parse(data);
      } catch (err) {}
      return ret;
    }

    this.selectBoard = function (name) {
      name = name || DEFAULT;
      var i;
      var selectedBoard = null;
      for (i in common.boards) {
        if (common.boards[i].name === name) {
          selectedBoard = common.boards[i];
          break;
        }
      }
      if (selectedBoard === null) {
        // Board not found: select default board
        for (i in common.boards) {
          if (common.boards[i].name === DEFAULT) {
            selectedBoard = common.boards[i];
            break;
          }
        }
      }
      common.selectedBoard = selectedBoard;
      common.pinoutInputHTML = generateHTMLOptions(
        common.selectedBoard.pinout,
        "input"
      );
      common.pinoutOutputHTML = generateHTMLOptions(
        common.selectedBoard.pinout,
        "output"
      );
      utils.rootScopeSafeApply();
      return common.selectedBoard;
    };

    this.boardLabel = function (name) {
      for (var i in common.boards) {
        if (common.boards[i].name === name) {
          return common.boards[i].info.label;
        }
      }
      return name;
    };

    function generateHTMLOptions(pinout, type) {
      var code = "<option></option>";
      for (var i in pinout) {
        if (pinout[i].type === type || pinout[i].type === "inout") {
          code +=
            '<option value="' +
            pinout[i].value +
            '">' +
            pinout[i].name +
            "</option>";
        }
      }
      return code;
    }
  });