-
Notifications
You must be signed in to change notification settings - Fork 3
/
champ.rb
executable file
·1066 lines (1003 loc) · 47.5 KB
/
champ.rb
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env ruby
require 'fileutils'
require 'open3'
require 'set'
require 'tmpdir'
require 'yaml'
require 'stringio'
KEYWORDS = <<EOS
ADC AND ASL BCC BCS BEQ BIT BMI BNE BPL BRA BRK BVC BVS CLC CLD CLI CLV
CMP CPX CPY DEA DEC DEX DEY DSK EOR EQU INA INC INX INY JMP JSR LDA LDX
LDY LSR MX NOP ORA ORG PHA PHP PHX PHY PLA PLP PLX PLY ROL ROR RTI RTS
SBC SEC SED SEI STA STX STY STZ TAX TAY TRB TSB TSX TXA TXS TYA
EOS
# font data borrowed from http://sunge.awardspace.com/glcd-sd/node4.html
# Graphic LCD Font (Ascii Charaters 0x20-0x7F)
# Author: Pascal Stang, Date: 10/19/2001
FONT_DATA = <<EOS
00 00 00 00 00 00 00 5F 00 00 00 07 00 07 00 14 7F 14 7F 14 24 2A 7F 2A 12
23 13 08 64 62 36 49 55 22 50 00 05 03 00 00 00 1C 22 41 00 00 41 22 1C 00
08 2A 1C 2A 08 08 08 3E 08 08 00 50 30 00 00 08 08 08 08 08 00 60 60 00 00
20 10 08 04 02 3E 51 49 45 3E 00 42 7F 40 00 42 61 51 49 46 21 41 45 4B 31
18 14 12 7F 10 27 45 45 45 39 3C 4A 49 49 30 01 71 09 05 03 36 49 49 49 36
06 49 49 29 1E 00 36 36 00 00 00 56 36 00 00 00 08 14 22 41 14 14 14 14 14
41 22 14 08 00 02 01 51 09 06 32 49 79 41 3E 7E 11 11 11 7E 7F 49 49 49 36
3E 41 41 41 22 7F 41 41 22 1C 7F 49 49 49 41 7F 09 09 01 01 3E 41 41 51 32
7F 08 08 08 7F 00 41 7F 41 00 20 40 41 3F 01 7F 08 14 22 41 7F 40 40 40 40
7F 02 04 02 7F 7F 04 08 10 7F 3E 41 41 41 3E 7F 09 09 09 06 3E 41 51 21 5E
7F 09 19 29 46 46 49 49 49 31 01 01 7F 01 01 3F 40 40 40 3F 1F 20 40 20 1F
7F 20 18 20 7F 63 14 08 14 63 03 04 78 04 03 61 51 49 45 43 00 00 7F 41 41
02 04 08 10 20 41 41 7F 00 00 04 02 01 02 04 40 40 40 40 40 00 01 02 04 00
20 54 54 54 78 7F 48 44 44 38 38 44 44 44 20 38 44 44 48 7F 38 54 54 54 18
08 7E 09 01 02 08 14 54 54 3C 7F 08 04 04 78 00 44 7D 40 00 20 40 44 3D 00
00 7F 10 28 44 00 41 7F 40 00 7C 04 18 04 78 7C 08 04 04 78 38 44 44 44 38
7C 14 14 14 08 08 14 14 18 7C 7C 08 04 04 08 48 54 54 54 20 04 3F 44 40 20
3C 40 40 20 7C 1C 20 40 20 1C 3C 40 30 40 3C 44 28 10 28 44 0C 50 50 50 3C
44 64 54 4C 44 00 08 36 41 00 00 00 7F 00 00 00 41 36 08 00 08 08 2A 1C 08
08 1C 2A 08 08
EOS
FONT = FONT_DATA.split(/\s+/).map { |x| x.strip }.reject { |x| x.empty? }.map { |x| x.to_i(16) }
class Champ
def initialize
if ARGV.empty?
STDERR.puts 'Usage: ./champ.rb [options] <config.yaml>'
STDERR.puts 'Options:'
STDERR.puts ' --max-frames <n>'
STDERR.puts ' --error-log-size <n> (default: 20)'
STDERR.puts ' --no-animation'
exit(1)
end
@have_dot = `dot -V 2>&1`.strip[0, 3] == 'dot'
@files_dir = 'report-files'
FileUtils.rm_rf(@files_dir)
FileUtils.mkpath(@files_dir)
@max_frames = nil
@record_frames = true
@cycles_per_function = {}
@execution_log = []
@execution_log_size = 20
@code_for_pc = {}
@source_for_file = {}
@max_source_width_for_file = {}
@pc_for_file_and_line = {}
args = ARGV.dup
while args.size > 1
item = args.shift
if item == '--max-frames'
@max_frames = args.shift.to_i
elsif item == '--error-log-size'
@execution_log_size = args.shift.to_i
elsif item == '--no-animation'
@record_frames = false
else
STDERR.puts "Invalid argument: #{item}"
exit(1)
end
end
config_path = args.shift
@config = YAML.load(File.read(config_path))
@source_files = []
@config['load'].each_pair do |address, path|
fixed_path = path.dup
unless fixed_path[0] == '/'
fixed_path = File.absolute_path(File.join(File.dirname(config_path), fixed_path))
end
@source_files << {
:path => fixed_path,
:address => address
}
end
@highlight_color = '#fce98d'
if @config['highlight']
@highlight_color = @config['highlight']
end
@histogram_color = '#12959f'
@keywords = Set.new(KEYWORDS.split(/\s/).map { |x| x.strip }.reject { |x| x.empty? })
@global_variables = {}
@watches = {}
@watches_for_index = []
@label_for_pc = {}
@pc_for_label = {}
# init disk image to zeroes
@disk_image = [0] * 0x10000
# load empty disk image
load_image('empty', 0)
@source_files.each do |source_file|
@source_path = File.absolute_path(source_file[:path])
@source_line = 0
unless File.exist?(@source_path)
STDERR.puts 'Input file not found.'
exit(1)
end
if @source_path[-2, 2] == '.s'
Dir::mktmpdir do |temp_dir|
FileUtils.cp(@source_path, temp_dir)
pwd = Dir.pwd
Dir.chdir(temp_dir)
# compile the source
merlin_output = `Merlin32 -V . #{File.basename(@source_path)}`
if $?.exitstatus != 0 || File.exist?('error_output.txt')
STDERR.puts merlin_output
exit(1)
end
# collect Merlin output files
@merlin_output_path = File.absolute_path(Dir['*_Output.txt'].first)
@merlin_binary_path = @merlin_output_path.sub('_Output.txt', '')
# parse Merlin output files
# TODO: Adjust addresses!!!
parse_merlin_output(@merlin_output_path)
load_image(@merlin_binary_path, source_file[:address])
Dir.chdir(pwd)
end
else
load_image(@source_path, source_file[:address])
end
end
end
def load_image(path, address)
# puts "[#{sprintf('0x%04x', address)} - #{sprintf('0x%04x', address + File.size(path) - 1)}] - loading #{File.basename(path)}"
File::binread(path).unpack('C*').each.with_index { |b, i| @disk_image[i + address] = b }
end
def run
Dir::mktmpdir do |temp_dir|
if @config.include?('instant_rts')
@config['instant_rts'].each do |label|
@disk_image[@pc_for_label[label]] = 0x60 # insert RTS
end
end
File.binwrite(File.join(temp_dir, 'disk_image'), @disk_image.pack('C*'))
# build watch input for C program
io = StringIO.new
watch_index = 0
@watches.keys.sort.each do |pc|
@watches[pc].each do |watch0|
watch0[:components].each do |watch|
if watch.include?(:register) || watch.include?(:address)
which = watch.include?(:register) ?
sprintf('reg,%s', watch[:register]) :
sprintf('mem,0x%04x', watch[:address])
io.puts sprintf('%d,0x%04x,%d,%s,%s',
watch_index, pc,
watch0[:post] ? 1 : 0,
watch[:type],
which)
end
end
@watches_for_index << watch0
watch_index += 1
end
end
watch_input = io.string
@watch_values = {}
@watch_called_from_subroutine = {}
start_pc = @pc_for_label[@config['entry']] || @config['entry']
Signal.trap('INT') do
puts 'Killing 65C02 profiler...'
throw :sigint
end
@frame_count = 0
cycle_count = 0
last_frame_time = 0
frame_cycles = []
@total_cycles_per_function = {}
@calls_per_function = {}
@call_graph_counts = {}
@max_cycle_count = 0
call_stack = []
last_call_stack_cycles = 0
Open3.popen2("./p65c02 #{@record_frames ? '' : '--no-screen'} --start-pc #{start_pc} #{File.join(temp_dir, 'disk_image')}") do |stdin, stdout, thread|
stdin.puts watch_input.split("\n").size
stdin.puts watch_input
stdin.close
catch :sigint do
gi = nil
go = nil
gt = nil
if @record_frames
gi, go, gt = Open3.popen2("./pgif 280 192 2 > #{File.join(@files_dir, 'frames.gif')}")
gi.puts '000000'
gi.puts 'ffffff'
end
stdout.each_line do |line|
# puts "> #{line}"
parts = line.split(' ')
if parts.first == 'error'
parts.shift
pc = parts.shift.to_i(16)
message = parts.join(' ')
@error = {:pc => pc, :message => message}
elsif parts.first == 'log'
parts.shift
log = parts.map { |x| x.to_i(16) }
@execution_log << log
while @execution_log.size > @execution_log_size
@execution_log.shift
end
elsif parts.first == 'jsr'
pc = parts[1].to_i(16)
cycles = parts[2].to_i
@max_cycle_count = cycles
@calls_per_function[pc] ||= 0
@calls_per_function[pc] += 1
calling_function = start_pc
unless call_stack.empty?
calling_function = call_stack.last
@total_cycles_per_function[call_stack.last] ||= 0
@total_cycles_per_function[call_stack.last] += cycles - last_call_stack_cycles
end
@call_graph_counts[calling_function] ||= {}
@call_graph_counts[calling_function][pc] ||= 0
@call_graph_counts[calling_function][pc] += 1
last_call_stack_cycles = cycles
call_stack << pc
elsif parts.first == 'rts'
cycles = parts[1].to_i
@max_cycle_count = cycles
last_cycles = @total_cycles_per_function[call_stack.last] || 0
unless call_stack.empty?
@total_cycles_per_function[call_stack.last] ||= 0
@total_cycles_per_function[call_stack.last] += cycles - last_call_stack_cycles
end
if @cycles_per_function.include?(call_stack.last)
@cycles_per_function[call_stack.last] << {
:call_cycles => @total_cycles_per_function[call_stack.last] - last_cycles,
:at_cycles => cycles
}
end
last_call_stack_cycles = cycles
call_stack.pop
elsif parts.first == 'watch'
watch_index = parts[2].to_i
cycles = parts[3].to_i
@max_cycle_count = cycles
@watch_called_from_subroutine[watch_index] ||= Set.new()
@watch_called_from_subroutine[watch_index] << parts[1].to_i(16)
@watch_values[watch_index] ||= []
watch_value_tuple = parts[4, parts.size - 4].map { |x| x.to_i }
@watch_values[watch_index] << {:tuple => watch_value_tuple, :cycles => cycles}
elsif parts.first == 'screen'
@frame_count += 1
print "\rFrames: #{@frame_count}, Cycles: #{cycle_count}"
this_frame_cycles = parts[1].to_i
@max_cycle_count = this_frame_cycles
frame_cycles << this_frame_cycles
if @record_frames
data = parts[2, parts.size - 2].map { |x| x.to_i }
gi.puts 'l'
(0...192).each do |y|
(0...280).each do |x|
b = (data[y * 40 + (x / 7)] >> (x % 7)) & 1
gi.print b
end
gi.puts
end
gi.puts "d #{(this_frame_cycles - last_frame_time) / 10000}"
end
last_frame_time = this_frame_cycles
if @max_frames && @frame_count >= @max_frames
break
end
elsif parts.first == 'cycles'
cycle_count = parts[1].to_i
@max_cycle_count = cycle_count
print "\rFrames: #{@frame_count}, Cycles: #{cycle_count}"
end
end
if @record_frames
gi.close
gt.join
end
end
end
puts
@cycles_per_frame = []
(2...frame_cycles.size).each do |i|
@cycles_per_frame << frame_cycles[i] - frame_cycles[i - 1]
end
end
end
def print_c(pixels, width, height, x, y, c, color)
if c.ord >= 0x20 && c.ord < 0x80
font_index = (c.ord - 0x20) * 5
(0...5).each do |px|
(0...7).each do |py|
if ((FONT[font_index + px] >> py) & 1) == 1
pixels[(y + py) * width + (x + px)] = color
end
end
end
end
end
def print_c_r(pixels, width, height, x, y, c, color)
if c.ord >= 0x20 && c.ord < 0x80
font_index = (c.ord - 0x20) * 5
(0...5).each do |px|
(0...7).each do |py|
if ((FONT[font_index + px] >> (6 - py)) & 1) == 1
pixels[(y + px) * width + (x + py)] = color
end
end
end
end
end
def print_s(pixels, width, height, x, y, s, color)
s.each_char.with_index do |c, i|
print_c(pixels, width, height, x + i * 6, y, c, color)
end
end
def print_s_r(pixels, width, height, x, y, s, color)
s.each_char.with_index do |c, i|
print_c_r(pixels, width, height, x, y + i * 6, c, color)
end
end
def write_report
html_name = 'report.html'
print "Writing report to file://#{File.absolute_path(html_name)} ..."
File::open(html_name, 'w') do |f|
report = DATA.read
# write frames
io = StringIO.new
if @record_frames
io.puts "<img class='screenshot' src='#{File.join(@files_dir, 'frames.gif')}' /><br />"
end
if @cycles_per_frame.size > 0
io.puts '<p>'
io.puts "Frames recorded: #{@frame_count}<br />"
io.puts "Average cycles/frame: #{@cycles_per_frame.inject(0) { |sum, x| sum + x } / @cycles_per_frame.size}<br />"
io.puts '<p>'
end
report.sub!('#{screenshots}', io.string)
# write watches
io = StringIO.new
@watches_for_index.each.with_index do |watch, index|
io.puts "<div style='display: inline-block;'>"
if @watch_values.include?(index) || @cycles_per_function.include?(watch[:pc])
pixels = nil
width = nil
height = nil
histogram = {}
histogram_x = {}
histogram_y = {}
mask = [
[0,0,1,1,1,0,0],
[0,1,1,1,1,1,0],
[1,1,1,1,1,1,1],
[1,1,1,1,1,1,1],
[1,1,1,1,1,1,1],
[0,1,1,1,1,1,0],
[0,0,1,1,1,0,0]
]
if @watch_values.include?(index)
@watch_values[index].each do |item|
normalized_item = []
if item[:tuple].size == 1
normalized_item << (item[:cycles] * 255 / @max_cycle_count).to_i
end
item[:tuple].each.with_index do |x, i|
if watch[:components][i][:type] == 's8'
x += 128
elsif watch[:components][i][:type] == 'u16'
x >>= 8
elsif watch[:components][i][:type] == 's16'
x = (x + 32768) >> 8
end
normalized_item << x
end
offset = normalized_item.reverse.inject(0) { |x, y| (x << 8) + y }
histogram[offset] ||= 0
histogram[offset] += 1
histogram_x[normalized_item[0]] ||= 0
histogram_x[normalized_item[0]] += 1
histogram_y[normalized_item[1]] ||= 0
histogram_y[normalized_item[1]] += 1
end
else
max_cycle_count_for_function = @cycles_per_function[watch[:pc]].map do |x|
x[:call_cycles]
end.max
@cycles_per_function[watch[:pc]].each do |entry|
normalized_item = []
normalized_item << (entry[:at_cycles] * 255 / @max_cycle_count).to_i
normalized_item << (entry[:call_cycles] * 255 / max_cycle_count_for_function).to_i
offset = normalized_item.reverse.inject(0) { |x, y| (x << 8) + y }
histogram[offset] ||= 0
histogram[offset] += 1
histogram_x[normalized_item[0]] ||= 0
histogram_x[normalized_item[0]] += 1
histogram_y[normalized_item[1]] ||= 0
histogram_y[normalized_item[1]] += 1
end
end
histogram_max = histogram.values.max
histogram_x_max = histogram_x.values.max
histogram_y_max = histogram_y.values.max
canvas_width = 200
canvas_height = 200
histogram_height = 32
canvas_top = 10 + histogram_height
canvas_left = 30
canvas_right = 10 + histogram_height
canvas_bottom = 50
width = canvas_width + canvas_left + canvas_right
height = canvas_height + canvas_top + canvas_bottom
pixels = [0] * width * height
histogram.each_pair do |key, value|
x = key & 0xff;
y = ((key >> 8) & 0xff) ^ 0xff
x = (x * canvas_width) / 255 + canvas_left
y = (y * canvas_height) / 255 + canvas_top
(0..6).each do |dy|
py = y + dy - 3
if py >= 0 && py < height
(0..6).each do |dx|
next if mask[dy][dx] == 0
px = x + dx - 3
if px >= 0 && px < width
if pixels[py * width + px] == 0
pixels[py * width + px] = 1
end
end
end
end
end
pixels[y * width + x] = (((value.to_f / histogram_max) ** 0.5) * 63).to_i
end
if watch[:components].size > 1
# only show X histogram if it's not cycles
histogram_x.each_pair do |x, value|
x = (x * canvas_width) / 255 + canvas_left
normalized_value = (value.to_f / histogram_x_max * 31).to_i
(0..normalized_value).each do |dy|
pixels[(canvas_top - dy - 4) * width + x] = normalized_value - dy + 0x40
end
end
end
histogram_y.each_pair do |y, value|
y = ((y ^ 0xff) * canvas_height) / 255 + canvas_top
normalized_value = (value.to_f / histogram_y_max * 31).to_i
(0..normalized_value).each do |dx|
pixels[y * width + canvas_left + canvas_width + dx + 4] |= normalized_value - dx + 0x40
end
end
watch[:components].each.with_index do |component, component_index|
labels = []
if component[:type] == 'u8'
labels << [0.0, '0']
labels << [64.0/255, '64']
labels << [128.0/255, '128']
labels << [192.0/255, '192']
labels << [1.0, '255']
elsif component[:type] == 's8'
labels << [0.0, '-128']
labels << [64.0/255, '-64']
labels << [128.0/255, '0']
labels << [192.0/255, '64']
labels << [1.0, '127']
elsif component[:type] == 'u16'
labels << [0.0, '0']
labels << [64.0/255, '16k']
labels << [128.0/255, '32k']
labels << [192.0/255, '48k']
labels << [1.0, '64k']
elsif component[:type] == 's16'
labels << [0.0, '-32k']
labels << [64.0/255, '-16k']
labels << [128.0/255, '0']
labels << [192.0/255, '16k']
labels << [1.0, '32k']
end
labels.each do |label|
s = label[1]
if component_index == 0 && watch[:components].size == 2
x = (label[0] * canvas_width).to_i + canvas_left
print_s(pixels, width, height,
(x - s.size * (6 * label[0])).to_i,
canvas_top + canvas_height + 7, s, 31)
(0..(canvas_height + 3)).each do |y|
pixels[(y + canvas_top) * width + x] |= 0x20
end
else
y = ((1.0 - label[0]) * canvas_height).to_i + canvas_top
print_s_r(pixels, width, height, canvas_left - 12,
(y - s.size * (6 * (1.0 - label[0]))).to_i, s, 31)
(-3..canvas_width).each do |x|
pixels[y * width + (x + canvas_left)] |= 0x20
end
end
end
(0..0).each do |offset|
component_label = component[:name]
if component_index == 0 && watch[:components].size == 2
print_s(pixels, width, height,
(canvas_left + canvas_width * 0.5 - component_label.size * 3 + offset).to_i,
canvas_top + canvas_height + 18,
component_label, 31)
else
print_s_r(pixels, width, height,
canvas_left - 22,
(canvas_top + canvas_height * 0.5 - component_label.size * 3 + offset).to_i,
component_label, 31)
end
end
end
label = "#{sprintf('0x%04x', watch[:pc])} / #{watch[:path]}:#{watch[:line_number]}"
if @watch_values.include?(index)
label += " (#{watch[:post] ? 'post' : 'pre'})"
end
print_s(pixels, width, height, width / 2 - 3 * label.size, height - 20, label, 31)
if @watch_values.include?(index)
label = @watch_called_from_subroutine[index].map do |x|
"#{@label_for_pc[x] || sprintf('0x%04x', x)}+#{watch[:pc] - x}"
end.join(', ')
label = "at #{label}"
print_s(pixels, width, height, width / 2 - 3 * label.size, height - 10, label, 31)
end
if watch[:components].size == 1
# this watch is 1D, add X axis labels for cycles
labels = []
labels << [0.0, '0']
format_str = '%d'
divisor = 1
if @max_cycle_count >= 1e6
format_str = '%1.1fM'
divisor = 1e6
elsif @max_cycle_count > 1e3
format_str = '%1.1fk'
divisor = 1e3
end
# labels << [1.0, sprintf(format_str, (@max_cycle_count.to_f / divisor)).sub('.0', '')]
remaining_space = canvas_width - labels.inject(0) { |a, b| a + b.size * 6 }
space_per_label = sprintf(format_str, (@max_cycle_count.to_f / divisor)).sub('.0', '').size * 6 * 2
max_tween_labels = remaining_space / space_per_label
step = ((@max_cycle_count / max_tween_labels).to_f / divisor).ceil
step = 1 if step == 0 # prevent infinite loop!
x = step
while x < @max_cycle_count / divisor
labels << [x.to_f * divisor / @max_cycle_count, sprintf(format_str, x).sub('.0', '')]
x += step
end
labels.each do |label|
s = label[1]
x = (label[0] * canvas_width).to_i + canvas_left
print_s(pixels, width, height,
(x - s.size * (6 * label[0])).to_i,
canvas_top + canvas_height + 7, s, 31)
(0..(canvas_height + 3)).each do |y|
pixels[(y + canvas_top) * width + x] |= 0x20
end
end
(0..0).each do |offset|
component_label = 'cycles'
print_s(pixels, width, height,
(canvas_left + canvas_width * 0.5 - component_label.size * 3 + offset).to_i,
canvas_top + canvas_height + 18,
component_label, 31)
end
end
if (!@watch_values.include?(index)) && @cycles_per_function.include?(watch[:pc]) && (!@cycles_per_function[watch[:pc]].empty?)
max_cycle_count_for_function = @cycles_per_function[watch[:pc]].map do |x|
x[:call_cycles]
end.max
# this is a subroutine cycles watch, add Y axis labels for subroutine cycles
labels = []
labels << [0.0, '0']
format_str = '%d'
divisor = 1
if max_cycle_count_for_function >= 1e6
format_str = '%1.1fM'
divisor = 1e6
elsif max_cycle_count_for_function > 1e3
format_str = '%1.1fk'
divisor = 1e3
end
# labels << [1.0, sprintf(format_str, (max_cycle_count_for_function.to_f / divisor)).sub('.0', '')]
remaining_space = canvas_width - labels.inject(0) { |a, b| a + b.size * 6 }
space_per_label = sprintf(format_str, (max_cycle_count_for_function.to_f / divisor)).sub('.0', '').size * 6 * 2
max_tween_labels = remaining_space / space_per_label
step = ((max_cycle_count_for_function / max_tween_labels).to_f / divisor).ceil
step = 1 if step == 0 # prevent infinite loop!
x = step
while x < max_cycle_count_for_function / divisor
labels << [x.to_f * divisor / max_cycle_count_for_function, sprintf(format_str, x).sub('.0', '')]
x += step
end
labels.each do |label|
s = label[1]
y = ((1.0 - label[0]) * canvas_height).to_i + canvas_top
print_s_r(pixels, width, height, canvas_left - 12,
(y - s.size * (6 * (1.0 - label[0]))).to_i, s, 31)
(-3..canvas_width).each do |x|
pixels[y * width + (x + canvas_left)] |= 0x20
end
end
end
tr = @highlight_color[1, 2].to_i(16)
tg = @highlight_color[3, 2].to_i(16)
tb = @highlight_color[5, 2].to_i(16)
hr = @histogram_color[1, 2].to_i(16)
hg = @histogram_color[3, 2].to_i(16)
hb = @histogram_color[5, 2].to_i(16)
if pixels
colors_used = 32 * 3
gi, go, gt = Open3.popen2("./pgif #{width} #{height} #{colors_used}")
palette = [0] * colors_used
(0...32).each do |i|
if (i == 0)
r = 0xff
g = 0xff
b = 0xff
else
l = (((31 - i) + 1) << 3) - 1
r = l * tr / 255
g = l * tg / 255
b = l * tb / 255
end
palette[i] = sprintf('%02x%02x%02x', r, g, b)
r = r * 4 / 5
g = g * 4 / 5
b = b * 4 / 5
palette[i + 32] = sprintf('%02x%02x%02x', r, g, b)
fade = (i.to_f / 31) * 0.5 + 0.5
xr = (hr * fade + 0xff * (1.0 - fade)).to_i
xg = (hg * fade + 0xff * (1.0 - fade)).to_i
xb = (hb * fade + 0xff * (1.0 - fade)).to_i
palette[i + 64] = sprintf('%02x%02x%02x', xr, xg, xb)
end
gi.puts palette.join("\n")
gi.puts 'f'
gi.puts pixels.map { |x| sprintf('%02x', x) }.join("\n")
gi.close
gt.join
watch_path = File.join(@files_dir, "watch_#{index}.gif")
File::open(watch_path, 'w') do |f|
f.write go.read
end
io.puts "<img src='#{watch_path}'></img>"
end
else
io.puts "<em>No values recorded.</em>"
end
io.puts "</div>"
end
report.sub!('#{watches}', io.string)
if @cycles_per_function.empty?
report.sub!('#{cycle_watches}', '')
else
io = StringIO.new
report.sub!('#{cycle_watches}', io.string)
end
# write cycles
io = StringIO.new
io.puts "<table>"
io.puts "<thead>"
io.puts "<tr>"
io.puts "<th>Addr</th>"
io.puts "<th>CC</th>"
io.puts "<th>CC %</th>"
io.puts "<th>Calls</th>"
io.puts "<th>CC/Call</th>"
io.puts "<th>Label</th>"
io.puts "</tr>"
io.puts "</thead>"
cycles_sum = @total_cycles_per_function.values.inject(0) { |a, b| a + b }
@total_cycles_per_function.keys.sort do |a, b|
@total_cycles_per_function[b] <=> @total_cycles_per_function[a]
end.each do |pc|
io.puts "<tr>"
io.puts "<td>#{sprintf('0x%04x', pc)}</td>"
io.puts "<td style='text-align: right;'>#{@total_cycles_per_function[pc]}</td>"
io.puts "<td style='text-align: right;'>#{sprintf('%1.2f%%', @total_cycles_per_function[pc].to_f * 100.0 / cycles_sum)}</td>"
io.puts "<td style='text-align: right;'>#{@calls_per_function[pc]}</td>"
io.puts "<td style='text-align: right;'>#{@total_cycles_per_function[pc] / @calls_per_function[pc]}</td>"
io.puts "<td>#{@label_for_pc[pc]}</td>"
io.puts "</tr>"
end
io.puts "</table>"
report.sub!('#{cycles}', io.string)
if @have_dot
# render call graph
all_nodes = Set.new()
@call_graph_counts.each_pair do |key, entries|
all_nodes << key
entries.keys.each do |other_key|
all_nodes << other_key
end
end
io = StringIO.new
io.puts "digraph {"
io.puts "overlap = false;"
io.puts "rankdir = LR;"
io.puts "splines = true;"
io.puts "graph [fontname = Arial, fontsize = 8, size = \"14, 11\", nodesep = 0.2, ranksep = 0.3, ordering = out];"
io.puts "node [fontname = Arial, fontsize = 8, shape = rect, style = filled, fillcolor = \"#fce94f\" color = \"#c4a000\"];"
io.puts "edge [fontname = Arial, fontsize = 8, color = \"#444444\"];"
all_nodes.each do |node|
label = @label_for_pc[node] || sprintf('0x%04x', node)
label = "<B>#{label}</B>"
if @calls_per_function[node] && @total_cycles_per_function[node]
label += "<BR/>#{@total_cycles_per_function[node] / @calls_per_function[node]}"
end
io.puts " _#{node} [label = <#{label}>];"
end
max_call_count = 0
@call_graph_counts.each_pair do |key, entries|
entries.values.each do |count|
max_call_count = count if count > max_call_count
end
end
@call_graph_counts.each_pair do |key, entries|
entries.keys.each do |other_key|
penwidth = 0.5 + ((entries[other_key].to_f / max_call_count) ** 0.3) * 2
io.puts "_#{key} -> _#{other_key} [label = \"#{entries[other_key]}x\", penwidth = #{penwidth}];"
end
end
io.puts "}"
dot = io.string
svg = Open3.popen2('dot -Tsvg') do |stdin, stdout, thread|
stdin.print(dot)
stdin.close
stdout.read
end
File::open(File.join(@files_dir, 'call_graph.svg'), 'w') do |f|
f.write(svg)
end
report.sub!('#{call_graph}', svg)
else
report.sub!('#{call_graph}', '<em>(GraphViz not installed)</em>')
end
# write error dump
if @error
io = StringIO.new
io.puts "<div style='float: left; margin-right: 10px;'>"
io.puts "<h2>Source code</h2>"
io.puts "<code><pre>"
source_code = @code_for_pc[@error[:pc]]
offset = source_code[:line] - 1
this_filename = source_code[:file]
format_str = "%-#{@max_source_width_for_file[this_filename]}s"
io.puts "<span class='heading'> Line | PC | #{sprintf(format_str, this_filename)}</span>"
((offset - 16)..(offset + 16)).each do |i|
next if i < 0 || i >= @source_for_file[this_filename].size
io.print "<span class='#{(i == offset) ? 'error' : 'code'}'>"
line_pc = nil
if @pc_for_file_and_line[this_filename]
if @pc_for_file_and_line[this_filename][i + 1]
line_pc = sprintf('0x%04x', @pc_for_file_and_line[this_filename][i + 1])
end
end
line_pc ||= ''
io.print sprintf("%5d | %6s | %-#{@max_source_width_for_file[this_filename]}s", i + 1, line_pc, @source_for_file[this_filename][i])
io.print "</span>" if i == offset
io.puts
end
io.puts "</pre></code>"
io.puts "</div>"
io.puts "<div style='display: inline-block;'>"
io.puts "<h2>Execution log</h2>"
io.puts "<code><pre>"
io.puts sprintf("<span class='heading'> PC | A X Y PC SP Flags </span>")
@execution_log.each do |item|
io.puts sprintf("<span class='code'> 0x%04x | 0x%02x 0x%02x 0x%02x 0x%04x 0x%02x 0x%02x </span>", *item)
end
io.puts sprintf("<span class='error'> 0x%04x | %-37s </span>", @error[:pc], @error[:message])
io.puts "</pre></code>"
io.puts "</div>"
io.puts "<div style='clear: both;'></div>"
report.sub!('#{error}', io.string)
else
report.sub!('#{error}', '')
end
f.puts report
end
puts ' done.'
end
def parse_asm_int(s)
if s[0] == '#'
s[1, s.size - 1].to_i
elsif s[0] == '$'
s[1, s.size - 1].to_i(16)
else
s.to_i
end
end
def parse_merlin_output(path)
input_file = File.basename(@source_path)
@source_for_file[input_file] = File.read(@source_path).split("\n").map { |x| x.gsub("\t", ' ' * 4) }
@max_source_width_for_file[input_file] = [@source_for_file[input_file].map { |x| x.size }.max, 40].max
@source_line = -3
File.open(path, 'r') do |f|
f.each_line do |line|
@pc_for_file_and_line[input_file] ||= {}
@source_line += 1
parts = line.split('|')
next unless parts.size > 2
line_type = parts[2].strip
if ['Code', 'Equivalence', 'Empty'].include?(line_type)
next if parts[6].nil?
next unless parts[6] =~ /[0-9A-F]{2}\/[0-9A-F]{4}/
pc = parts[6].split(' ').first.split('/').last.to_i(16)
code = parts[7].strip
code_parts = code.split(/\s+/)
line_number = parts[1].split(' ').map { |x| x.strip }.reject { |x| x.empty? }.last.to_i
@code_for_pc[pc] = {
:file => input_file,
:line => line_number,
}
@pc_for_file_and_line[input_file][line_number] = pc
next if code_parts.empty?
label = nil
champ_directives = []
if code.include?(';')
comment = code[code.index(';') + 1, code.size].strip
comment.scan(/@[^;][^\s]+/).each do |match|
champ_directives << match.to_s
end
end
if champ_directives.empty? && line_type == 'Equivalence'
label = code_parts[0]
pc = parse_asm_int(code_parts[2])
@label_for_pc[pc] = label
@pc_for_label[label] = pc
else
unless @keywords.include?(code_parts.first)
label = code_parts.first
code = code.sub(label, '').strip
@label_for_pc[pc] = label
@pc_for_label[label] = pc
end
end
next if champ_directives.empty?
if line_type == 'Equivalence'
if champ_directives.size != 1
fail('No more than one champ directive allowed in equivalence declaration.')
end
item = {
:address => code_parts[2].sub('$', '').to_i(16),
:type => parse_champ_directive(champ_directives.first, true)[:type]
}
@global_variables[label] = item
elsif line_type == 'Code'
@watches[pc] ||= []
champ_directives.each do |directive|
watch = parse_champ_directive(directive, false)
watch[:line_number] = line_number
watch[:pc] = pc
if watch[:subroutine_cycles]
watch[:components].first[:name] = "#{@label_for_pc[pc] || sprintf('0x%04x', pc)} cycles"
@cycles_per_function[pc] = []
end
@watches[pc] << watch
end
end
else
if line.include?(';')
if line.split(';').last.include?('@')
fail('Champ directive not allowed here.')
end
end
end
end
end
end
def fail(message)
STDERR.puts sprintf('[%s:%d] %s', File::basename(@source_path), @source_line, message)
exit(1)
end
def parse_champ_directive(s, global_variable = false)
original_directive = s.dup
# Au Au(post) As Xs(post) RX u8 Au,Xu,Yu
result = {}
s = s[1, s.size - 1] if s[0] == '@'
result[:path] = File.basename(@source_path)
if global_variable
if ['u8', 's8', 'u16', 's16'].include?(s)
result[:type] = s
else
fail("Error parsing champ directive: #{original_directive}")
end
else
if s == 'cycles'
result[:subroutine_cycles] = true
result[:components] = [
{:subroutine_cycles => true}
]
return result
end
if s.include?('(post)')
result[:post] = true
s.sub!('(post)', '')
end
result[:components] = s.split(',').map do |part|
part_result = {}
if ['Au', 'As', 'Xu', 'Xs', 'Yu', 'Ys'].include?(part[0, 2])
part_result[:register] = part[0]
part_result[:name] = part[0]
part_result[:type] = part[1] + '8'
elsif @global_variables.include?(part)
part_result[:address] = @global_variables[part][:address]
part_result[:type] = @global_variables[part][:type]
part_result[:name] = part
else
fail("Error parsing champ directive: #{original_directive}")
exit(1)
end
part_result
end
if result[:components].size > 2
fail("No more than two components allowed per watch in #{original_directive}")
end
end
result
end
end
['p65c02', 'pgif'].each do |file|
unless FileUtils.uptodate?(file, ["#{file}.c"])
system("gcc -o #{file} #{file}.c")
unless $?.exitstatus == 0
exit(1)
end
end
end