OpenCores
URL https://opencores.org/ocsvn/8051/8051/trunk

Subversion Repositories 8051

[/] [8051/] [tags/] [rel_12/] [sw/] [source/] [uMain.pas] - Blame information for rev 186

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 48 simont
unit uMain;
2
 
3
interface
4
 
5
uses
6
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
7
  StdCtrls, ExtCtrls, Buttons, inifiles;
8
 
9
type
10
  tbytes = array [0..3] of array [0..7] of boolean;
11
  tstr = array [0..7] of string;
12
  tRom = record
13
    bytes: tbytes;
14
    str: tstr;
15
  end;
16
 
17
  TForm1 = class(TForm)
18
    od1: TOpenDialog;
19
    rgOut: TRadioGroup;
20
    edtIn: TEdit;
21
    edtOut: TEdit;
22
    Label1: TLabel;
23
    Label2: TLabel;
24
    sbIn: TSpeedButton;
25
    sbOut: TSpeedButton;
26
    btnMake: TBitBtn;
27
    btnExit: TBitBtn;
28
    sd1: TSaveDialog;
29
    Panel1: TPanel;
30
    Memo1: TMemo;
31
    procedure Button1Click(Sender: TObject);
32
    procedure btnExitClick(Sender: TObject);
33
    procedure btnMakeClick(Sender: TObject);
34
    procedure sbInClick(Sender: TObject);
35
    procedure sbOutClick(Sender: TObject);
36
    procedure FormCreate(Sender: TObject);
37
    procedure FormDestroy(Sender: TObject);
38
  private
39
    { Private declarations }
40
    roms: array [0..2047] of tRom;
41
    function makeV(hFile:string): string;
42
    function hexToInt (hex:string):integer;
43
    function intToHex (int:integer):string;
44
    function inToBits (int:integer): string;
45
    procedure addB(rom, byt: integer; num:string);
46
    procedure addS(romN:integer);
47
    procedure simul (hex: string; str: tstrings);
48
    procedure simulIn (hex: string; str: tstrings);
49
    procedure syn (hex: string; str: tstrings);
50
    function n2 (n: integer):integer;
51
  public
52
    { Public declarations }
53
  end;
54
 
55
var
56
  Form1: TForm1;
57
 
58
implementation
59
 
60
{$R *.DFM}
61
 
62
procedure TForm1.Button1Click(Sender: TObject);
63
begin
64
  if od1.Execute then
65
    makeV(od1.FileName);
66
end;
67
 
68
//
69
function TForm1.makeV(hFile:string): string;
70
var str: TstringList;
71
    res, s: string;
72
    j, i, byts: integer;
73
    oaddr, naddr: longint;
74
begin
75
  try
76
    str:= tstringlist.create;
77
    str.LoadFromFile(hFile);
78
    res:= '';
79
    oaddr:=0;
80
 
81
    for i:=0 to str.Count-1 do begin
82
      byts:=(hexToInt(str[i][2])*16)+hexToInt(str[i][3]);
83
      naddr:=(hexToInt(str[i][4])*64)+(hexToInt(str[i][5])*32)+(hexToInt(str[i][6])*16)+hexToInt(str[i][7]);
84
      if ((naddr+byts)>0) then
85
        str[i]:=copy(str[i], 4, 4)+copy(str[i], 2, 2)+copy(str[i], 10,byts*2)
86
      else begin
87 61 simont
        str[i]:='x';
88 48 simont
        break;
89
      end;
90
    end;
91
    str.sort;
92
 
93
    for i:=0 to str.Count-1 do begin
94 61 simont
      if str[i][1]='x' then break;
95 48 simont
      byts:=(hexToInt(str[i][5])*16)+hexToInt(str[i][6]);
96
      naddr:=(hexToInt(str[i][1])*4096)+(hexToInt(str[i][2])*256)+(hexToInt(str[i][3])*16)+hexToInt(str[i][4]);
97
      s:='';
98
      if oaddr<>naddr then begin
99
        for j:=0 to naddr-oaddr-1 do begin
100
          s:= s+'00';
101
        end;
102
      end;
103
      res:=res+s+copy(str[i], 7,byts*2);
104
      oaddr:= naddr+byts
105
    end;
106
    str.free;
107
    result:=inToBits(oaddr)+res;
108
  except
109
    on e:exception do showmessage(e.message)
110
  end
111
end;
112
 
113
function TForm1.hexToInt (hex:string):integer;
114
begin
115
  if UpperCase(hex)='A' then begin result:= 10; exit end;
116
  if UpperCase(hex)='B' then begin result:= 11; exit end;
117
  if UpperCase(hex)='C' then begin result:= 12; exit end;
118
  if UpperCase(hex)='D' then begin result:= 13; exit end;
119
  if UpperCase(hex)='E' then begin result:= 14; exit end;
120
  if UpperCase(hex)='F' then begin result:= 15; exit end;
121
  result:= strToInt(hex)
122
end;
123
 
124
procedure TForm1.addB(rom, byt: integer; num:string);
125
var int: integer;
126
begin
127
 
128
  int:=hexToInt(num[1]);
129
  if int>7 then begin
130
    roms[rom].bytes[byt][7]:=true;
131
    int:= int-8;
132
  end else
133
    roms[rom].bytes[byt][7]:=false;
134
  if int>3 then begin
135
    roms[rom].bytes[byt][6]:=true;
136
    int:= int-4;
137
  end else
138
    roms[rom].bytes[byt][6]:=false;
139
  if int>1 then begin
140
    roms[rom].bytes[byt][5]:=true;
141
    int:= int-2;
142
  end else
143
    roms[rom].bytes[byt][5]:=false;
144
  if int=1 then begin
145
    roms[rom].bytes[byt][4]:=true;
146
  end else
147
    roms[rom].bytes[byt][4]:=false;
148
 
149
  int:=hexToInt(num[2]);
150
  if int>7 then begin
151
    roms[rom].bytes[byt][3]:=true;
152
    int:= int-8;
153
  end else
154
    roms[rom].bytes[byt][3]:=false;
155
  if int>3 then begin
156
    roms[rom].bytes[byt][2]:=true;
157
    int:= int-4;
158
  end else
159
    roms[rom].bytes[byt][2]:=false;
160
  if int>1 then begin
161
    roms[rom].bytes[byt][1]:=true;
162
    int:= int-2;
163
  end else
164
    roms[rom].bytes[byt][1]:=false;
165
  if int=1 then begin
166
    roms[rom].bytes[byt][0]:=true;
167
  end else
168
    roms[rom].bytes[byt][0]:=false;
169
 
170
end;
171
 
172
procedure TForm1.addS(romN:integer);
173
var r, i, j, num: integer;
174
begin
175
  for r:=0 to romN do begin
176
    for i:=0 to 7 do begin
177
      num:=0;
178
      if roms[r].bytes[3][i] then num:=num+8;
179
      if roms[r].bytes[2][i] then num:=num+4;
180
      if roms[r].bytes[1][i] then num:=num+2;
181
      if roms[r].bytes[0][i] then num:=num+1;
182
      for j:=0 to 3 do roms[r].bytes[j][i]:= false;
183
//if r=0 then memo1.lines.add(intToHex(num));
184
      roms[r].Str[i]:=intToHex(num)+roms[r].Str[i];
185
    end;
186
  end;
187
end;
188
 
189
function TForm1.intToHex (int:integer):string;
190
begin
191
  case int of
192
    15: result:='f';
193
    14: result:='e';
194
    13: result:='d';
195
    12: result:='c';
196
    11: result:='b';
197
    10: result:='a';
198
  else
199
     result:=IntToStr(int);
200
  end;
201
end;
202
 
203
procedure TForm1.simul (hex: string; str: tstrings);
204
var i, j: integer;
205
//    str: tstrings;
206
    s: string;
207
begin
208
  str.Clear;
209
 
210
  str.Add('');
211
  str.Add('///');
212
  str.Add('/// created by oc8051 rom maker');
213
  str.Add('/// author: Simon Teran (simont@opencores.org)');
214
  str.Add('///');
215
  str.Add('/// source file: '+edtIn.Text);
216
  str.Add('/// date: '+ datetoStr(date));
217
  str.Add('/// time: '+ timeToStr(time));
218
  str.Add('///');
219
  str.Add('');
220
  str.Add('module oc8051_rom (rst, clk, addr, ea_int, data1, data2, data3);');
221
  str.Add('');
222
  str.Add('parameter INT_ROM_WID= '+intToStr(strToInt(copy(hex,1,2))) +';');
223
  str.Add('');
224
  str.Add('input rst, clk;');
225
  str.Add('input [15:0] addr;');
226
  str.Add('output ea_int;');
227
  str.Add('output [7:0] data1, data2, data3;');
228
  str.Add('reg ea_int;');
229
  str.Add('reg [7:0] data1, data2, data3;');
230
  str.Add('reg [7:0] buff [65535:0];');
231
  str.Add('integer i;');
232
  str.Add('');
233
  str.Add('wire ea;');
234
  str.Add('');
235
  str.Add('assign ea = | addr[15:INT_ROM_WID];');
236
  str.Add('');
237
  str.Add('initial');
238
  str.Add('begin');
239
  str.Add('    for (i=0; i<65536; i=i+1)');
240
  str.Add('      buff [i] = 8''h00;');
241
  str.Add('#2');
242
  str.Add('');
243
 
244
  hex := copy(hex,3,length(hex)-2);
245
  for i:=0 to (length(hex) div 2)-1 do begin
246
    s:='    buff [16''h';
247
    s:=s+intToHex(i div 4096);
248
    j:=i mod 4096;
249
    s:=s+intToHex(j div 256);
250
    j:=j mod 256;
251
    s:=s+'_'+intToHex(j div 16)+intToHex(j mod 16);
252
    S:=s+'] = 8''h';
253
    S:=s+copy(Hex,(i*2)+1,2)+';';
254
    str.Add(s);
255
  end;
256
 
257
  str.Add('end');
258
  str.Add('');
259
  str.Add('always @(posedge clk)');
260
  str.Add('begin');
261
  str.Add('  data1 <= #1 buff [addr];');
262
  str.Add('  data2 <= #1 buff [addr+1];');
263
  str.Add('  data3 <= #1 buff [addr+2];');
264
  str.Add('end');
265
  str.Add('');
266
  str.Add('always @(posedge clk or posedge rst)');
267
  str.Add(' if (rst)');
268
  str.Add('   ea_int <= #1 1''b1;');
269
  str.Add('  else ea_int <= #1 !ea;');
270
  str.Add('');
271
  str.Add('endmodule');
272
 
273
//  memo1.Lines.Clear;
274
//  memo1.Lines.AddStrings(str);
275
 
276
end;
277
 
278
procedure TForm1.syn (hex: string; str: tstrings);
279
var addrw, i, cntR, cntB:integer;
280
    tmp, astr: string;
281
//    str:tstrings;
282
begin
283
  str.Add('');
284
  str.Add('///');
285
  str.Add('/// created by oc8051 rom maker');
286
  str.Add('/// author: Simon Teran (simont@opencores.org)');
287
  str.Add('///');
288
  str.Add('/// source file: '+edtIn.Text);
289
  str.Add('/// date: '+ datetoStr(date));
290
  str.Add('/// time: '+ timeToStr(time));
291
  str.Add('///');
292
  str.Add('');
293
  astr:= '';
294
  addrw:=strToInt(copy(hex,1,2))-1;
295
  str.add('module ROM32X1(O, A0, A1, A2, A3, A4); // synthesis syn_black_box syn_resources="luts=2"');
296
  str.add('output O;');
297
  for i:= 0 to 4 do
298
    str.add('input A'+IntToStr(i)+';');
299
  str.add('endmodule');
300
  str.add('');
301
  str.add('//rom for 8051 processor');
302
  str.add('');
303
 
304
  str.Add('module oc8051_rom (rst, clk, addr, ea_int, data1, data2, data3);');
305
  str.Add('');
306
  str.Add('parameter INT_ROM_WID= '+intToStr(strToInt(copy(hex,1,2))) +';');
307
  str.Add('');
308
  str.Add('input rst, clk;');
309
  str.Add('input [15:0] addr;');
310
  str.Add('output ea_int;');
311
  str.Add('output [7:0] data1, data2, data3;');
312
  str.Add('reg ea_int;');
313
  str.add('reg [4:0] addr01;');
314
  str.Add('reg [7:0] data1, data2, data3;');
315
  str.Add('');
316
  str.Add('wire ea;');
317
  str.add('wire [15:0] addr_rst;');
318
  tmp := 'wire [7:0] int_data0, int_data1, int_data2, int_data3';
319
  for i:= 4 to n2(addrw-5)-1 do tmp:= tmp+', int_data'+intToStr(i);
320
  str.add(tmp+';');
321
  str.Add('');
322
  str.Add('assign ea = | addr[15:INT_ROM_WID];');
323
  str.add('');
324
  str.add('assign addr_rst = rst ? 16''h0000 : addr;');
325
  str.add('');
326
  str.add('  rom0 rom_0 (.a(addr01), .o(int_data0));');
327
  str.add('  rom1 rom_1 (.a(addr01), .o(int_data1));');
328
  for i:=2 to n2(addrw-5)-1 do
329
    str.add('  rom'+IntToStr(i)+' rom_'+IntToStr(i)+' (.a(addr_rst['+IntToStr(addrw)+':'+IntToStr(addrw-4)+']), .o(int_data'+IntToStr(i)+'));');
330
  str.add('');
331
  str.add('always @(addr_rst)');
332
  str.add('begin');
333
  str.add('  if (addr_rst[1])');
334
  str.add('    addr01= addr_rst['+IntToStr(addrw)+':'+IntToStr(addrw-4)+']+ 5''h1;');
335
  str.add('  else');
336
  str.add('    addr01= addr_rst['+IntToStr(addrw)+':'+IntToStr(addrw-4)+'];');
337
  str.add('end');
338
  str.add('');
339
  str.add('//');
340
  str.add('// always read tree bits in row');
341
  str.add('always @(posedge clk)');
342
  str.add('begin');
343
  str.add('  case(addr['+IntToStr(addrw-5)+':0])');
344
  for i:=0 to n2(addrw-5)-1 do begin
345
    str.add('    '+IntToStr(addrw-4)+'''d'+intToStr(i)+': begin');
346
    str.add('      data1 <= #1 int_data'+intToStr(i)+';');
347
    str.add('      data2 <= #1 int_data'+intToStr((i+1) mod n2(addrw-5))+';');
348
    str.add('      data3 <= #1 int_data'+intToStr((i+2) mod n2(addrw-5))+';');
349
    str.add('   end');
350
  end;
351
  str.add('    default: begin');
352
  str.add('      data1 <= #1 8''h00;');
353
  str.add('      data2 <= #1 8''h00;');
354
  str.add('      data3 <= #1 8''h00;');
355
  str.add('     end');
356
  str.add('  endcase');
357
  str.add('end');
358
  str.add('');
359
  str.Add('always @(posedge clk or posedge rst)');
360
  str.Add(' if (rst)');
361
  str.Add('   ea_int <= #1 1''b1;');
362
  str.Add('  else ea_int <= #1 !ea;');
363
  str.Add('');
364
  str.add('endmodule');
365
  str.add('');
366
  str.add('');
367
 
368
  hex := copy(hex,3,length(hex)-2);
369
 
370
  //init roms
371
  for cntR:= 0 to n2(addrw-5)-1 do begin
372
    for cntB:= 0 to 7 do begin
373
      roms[cntR].str[cntB]:='';
374
    end;
375
  end;
376
 
377
  cntR:=0;
378
  cntB:=0;
379
  for i:=0 to (length(Hex) div 2)-1 do begin
380
    AddB(cntR, cntB, copy(Hex,(i*2)+1,2));
381
    if (cntB=3) and (cntR=n2(addrw-5)-1) then begin
382
      addS(n2(addrw-5)-1);
383
      cntR:=0;
384
      cntB:=0;
385
    end else begin
386
      if (cntR=n2(addrw-5)-1) then begin
387
        inc(cntB);
388
        cntR:=0;
389
      end else inc(cntR);
390
    end;
391
  end;
392
  addS(n2(addrw-5)-1);
393
 
394
 
395
  astr:='';
396
  for i:=length(roms[0].str[0]) to 7 do astr := astr+'0';
397
 
398
 
399
  tmp:='';
400
  for i:=0 to 4 do tmp:=tmp+',a['+IntToStr(i)+']';
401
 
402
  for cntR:= 0 to n2(addrw-5)-1 do begin
403
    str.add('//rom'+intToStr(cntR));
404
    str.add('module rom'+intToStr(cntR)+' (o,a);');
405
    str.add('input [4:0] a;');
406
    str.add('output [7:0] o;');
407
    for cntB:= 0 to 7 do begin
408
      str.add('ROM32X1 u'+intToStr(cntB)+' (o['+intToStr(cntB)+']'+tmp+') /* synthesis xc_props="INIT='+astr+roms[cntR].str[cntB]+'" */;');
409
    end;
410
    str.add('endmodule');
411
    str.add('');
412
  end;
413
 
414
end;
415
 
416
procedure TForm1.btnExitClick(Sender: TObject);
417
begin
418
  close;
419
end;
420
 
421
procedure TForm1.btnMakeClick(Sender: TObject);
422
var str:tstrings;
423
    s: string;
424
begin
425
  if not fileexists(edtIn.Text) then raise exception.Create('File '''+edtIn.Text+''' don''t exist!!');
426
  if edtOut.Text='' then raise exception.Create('Output file not....');
427
 
428
  str:= tstringlist.Create;
429
 
430
  s:=makeV(edtIn.Text);
431
  case rgout.ItemIndex of
432
    0: simul(s, str);
433
    1: syn(s, str);
434
    2: simulIn(s, str);
435
  end;
436
 
437
  str.SaveToFile(edtOut.Text);
438
  str.free;
439
 
440
  showmessage('OK');
441
end;
442
 
443
procedure TForm1.sbInClick(Sender: TObject);
444
begin
445
  if od1.Execute then begin
446
    edtIn.Text:= od1.FileName;
447
    if rgOut.ItemIndex=2 then
448
      edtOut.Text:= copy(od1.FileName, 1, length(od1.FileName)-3)+'in'
449
    else
450
      edtOut.Text:= copy(od1.FileName, 1, length(od1.FileName)-3)+'v';
451
  end
452
end;
453
 
454
procedure TForm1.sbOutClick(Sender: TObject);
455
begin
456
  if sd1.Execute then
457
    edtOut.Text:= sd1.FileName;
458
end;
459
 
460
function TForm1.inToBits (int:integer): string;
461
begin
462
  case int of
463
    0..127: result := '07';
464
    128..255: result := '08';
465
    256..511: result := '09';
466
    512..1023: result := '10';
467
    1024..2047: result := '11';
468
    2048..4095: result := '12';
469
    4096..8191: result := '13';
470
    8192..16383: result := '14';
471
    16384..32767: result := '15';
472
  else result := '16';
473
  end;
474
end;
475
 
476
function TForm1.n2 (n: integer):integer;
477
var i: integer;
478
begin
479
  result:=1;
480
  for i:=0 to n do result:= result * 2;
481
end;
482
 
483
procedure TForm1.FormCreate(Sender: TObject);
484
var ini: tInifile;
485
begin
486
  ini := TIniFile.Create('RomMaker.ini');
487
  edtIn.text:= ini.ReadString('settings', 'inFile', '');
488
  edtOut.text:= ini.ReadString('settings', 'OutFile', '');
489
  RgOut.ItemIndex:= ini.ReadInteger('settings', 'Select', 0);
490
  od1.FileName:=ini.ReadString('settings', 'inFile', '');
491
  ini.free;
492
end;
493
 
494
procedure TForm1.FormDestroy(Sender: TObject);
495
var ini: tInifile;
496
begin
497
  ini := TIniFile.Create('RomMaker.ini');
498
  ini.WriteString('settings', 'inFile', edtIn.text);
499
  ini.WriteString('settings', 'OutFile', edtOut.text);
500
  ini.WriteInteger('settings', 'Select', RgOut.ItemIndex);
501
  ini.free;
502
end;
503
 
504
procedure TForm1.simulIn (hex: string; str: tstrings);
505
var i: integer;
506
begin
507
  str.Clear;
508
  str.Add('///');
509
  str.Add('/// created by oc8051 rom maker');
510
  str.Add('/// author: Simon Teran (simont@opencores.org)');
511
  str.Add('///');
512
  str.Add('/// source file: '+edtIn.Text);
513
  str.Add('/// date: '+ datetoStr(date));
514
  str.Add('/// time: '+ timeToStr(time));
515
  str.Add('///');
516
 
517
  hex := copy(hex,3,length(hex)-2);
518
  for i:=0 to (length(hex) div 2)-1 do
519
    str.Add(copy(Hex,(i*2)+1,2));
520
//  for i:=(length(hex) div 2) to 65535 do
521
//    str.Add('00');
522
 
523
//  memo1.Lines.Clear;
524
//  memo1.Lines.AddStrings(str);
525
 
526
end;
527
 
528
end.

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.