clock-graph.5c revision 03b705cf
103b705cfSriastradhautoload Cairo;
203b705cfSriastradhimport Cairo;
303b705cfSriastradhlibrary "examples/sort.5c";
403b705cfSriastradhimport Sort;
503b705cfSriastradh
603b705cfSriastradhint width = 1000, height = 200;
703b705cfSriastradhint min_vco = 1400000000;
803b705cfSriastradhint max_vco = 2800000000;
903b705cfSriastradhint min = 0xffffffff;
1003b705cfSriastradhint max = 0;
1103b705cfSriastradh
1203b705cfSriastradhint max_clocks = 2000;
1303b705cfSriastradhint[4][max_clocks] clocks;
1403b705cfSriastradhint[4][max_clocks] vcos;
1503b705cfSriastradhint[4] clock_count = {0...};
1603b705cfSriastradh
1703b705cfSriastradhint[4] p2vals = {5,10,7,14};
1803b705cfSriastradh
1903b705cfSriastradhcairo_t cr = Cairo::new(width, height);
2003b705cfSriastradh
2103b705cfSriastradhvoid calc_p2(int p2i)
2203b705cfSriastradh{
2303b705cfSriastradh	int p2 = p2vals[p2i];
2403b705cfSriastradh	int min_p, max_p;
2503b705cfSriastradh
2603b705cfSriastradh	clocks[p2i] = (int [max_clocks]){0...};
2703b705cfSriastradh
2803b705cfSriastradh		if (p2 == 7 || p2 == 14) {
2903b705cfSriastradh		/* LVDS */
3003b705cfSriastradh		min_p = 7;
3103b705cfSriastradh		max_p = 98;
3203b705cfSriastradh	} else {
3303b705cfSriastradh		/* SDVO/DAC */
3403b705cfSriastradh		min_p = 5;
3503b705cfSriastradh		max_p = 80;
3603b705cfSriastradh	}
3703b705cfSriastradh
3803b705cfSriastradh	for (int m1 = 10; m1 <= 22; m1++) {
3903b705cfSriastradh		for (int m2 = 5; m2 <= 9; m2++) {
4003b705cfSriastradh			for (int n = 1; n <= 5; n++) {
4103b705cfSriastradh				for (int p1 = 1; p1 <= 8; p1++) {
4203b705cfSriastradh					int ref = 96000000;
4303b705cfSriastradh					int m = 5 * (m1 + 2) + (m2 + 2);
4403b705cfSriastradh					int p = p1 * p2;
4503b705cfSriastradh					int vco = floor(ref * m / (n + 2));
4603b705cfSriastradh					int clock = floor(vco / p);
4703b705cfSriastradh
4803b705cfSriastradh					if (p < min_p || p > max_p)
4903b705cfSriastradh						continue;
5003b705cfSriastradh					if (m < 70 || m > 120)
5103b705cfSriastradh						continue;
5203b705cfSriastradh					if (m2 > m1)
5303b705cfSriastradh						continue; /* won't happen */
5403b705cfSriastradh					if (vco < min_vco || vco > max_vco)
5503b705cfSriastradh						continue;
5603b705cfSriastradh
5703b705cfSriastradh/*
5803b705cfSriastradh					printf("clock: %d (%d,%d), %d, "
5903b705cfSriastradh						"(%d,%d)\n",
6003b705cfSriastradh						floor(clock / 1000),
6103b705cfSriastradh						m1, m2, n, p1, p2);
6203b705cfSriastradh*/
6303b705cfSriastradh
6403b705cfSriastradh					clocks[p2i][clock_count[p2i]] = clock;
6503b705cfSriastradh					vcos[p2i][clock_count[p2i]] = vco;
6603b705cfSriastradh					clock_count[p2i]++;
6703b705cfSriastradh				}
6803b705cfSriastradh			}
6903b705cfSriastradh		}
7003b705cfSriastradh	}
7103b705cfSriastradh}
7203b705cfSriastradh
7303b705cfSriastradhbool sort_p2(poly a, poly b)
7403b705cfSriastradh{
7503b705cfSriastradh	return a > b;
7603b705cfSriastradh}
7703b705cfSriastradh
7803b705cfSriastradhint min_rate = 25000 * 1000;
7903b705cfSriastradhint max_rate = 200000 * 1000;
8003b705cfSriastradh
8103b705cfSriastradhreal scale_x(real clock)
8203b705cfSriastradh{
8303b705cfSriastradh	int min_x = 75, max_x = width - 50;
8403b705cfSriastradh
8503b705cfSriastradh	real frac = (clock - min_rate) / (max_rate - min_rate);
8603b705cfSriastradh
8703b705cfSriastradh	return min_x + frac * (max_x - min_x);
8803b705cfSriastradh}
8903b705cfSriastradh
9003b705cfSriastradhfor (p2i = 0; p2i < dim(p2vals); p2i++) {
9103b705cfSriastradh	int p2 = p2vals[p2i]; 
9203b705cfSriastradh	calc_p2(p2i);
9303b705cfSriastradh	real row_y1 = (p2i + 1) / (dim(p2vals) + 1) * height;
9403b705cfSriastradh	real row_y2 = p2i / (dim(p2vals) + 1) * height;
9503b705cfSriastradh
9603b705cfSriastradh	/*qsort(&p2vals[p2i], sort_p2);*/
9703b705cfSriastradh
9803b705cfSriastradh	switch (p2) {
9903b705cfSriastradh	case 5:
10003b705cfSriastradh		set_source_rgb(cr, 1,0,0);
10103b705cfSriastradh		break;
10203b705cfSriastradh	case 10:
10303b705cfSriastradh		set_source_rgb(cr, 0,1,0);
10403b705cfSriastradh		break;
10503b705cfSriastradh	case 7:
10603b705cfSriastradh		set_source_rgb(cr, 0,0,1);
10703b705cfSriastradh		break;
10803b705cfSriastradh	case 14:
10903b705cfSriastradh		set_source_rgb(cr, 0,0,0);
11003b705cfSriastradh		break;
11103b705cfSriastradh	}
11203b705cfSriastradh
11303b705cfSriastradh	/* draw the line for the clock */
11403b705cfSriastradh	for (int i = 0; i < clock_count[p2i]; i++) {
11503b705cfSriastradh		int clock = clocks[p2i][i];
11603b705cfSriastradh		real xpos;
11703b705cfSriastradh
11803b705cfSriastradh		if (clock < min_rate || clock > max_rate)
11903b705cfSriastradh			continue;
12003b705cfSriastradh
12103b705cfSriastradh		xpos = scale_x(clock);
12203b705cfSriastradh		move_to(cr, xpos, row_y1);
12303b705cfSriastradh		line_to(cr, xpos, row_y2);
12403b705cfSriastradh		stroke(cr);
12503b705cfSriastradh	}
12603b705cfSriastradh
12703b705cfSriastradh	set_source_rgb(cr, 1, 1, 1);
12803b705cfSriastradh	/* add a mark for the vco value of the clocks at each location */
12903b705cfSriastradh	for (int i = 0; i < clock_count[p2i]; i++) {
13003b705cfSriastradh		int clock = clocks[p2i][i];
13103b705cfSriastradh		int vco = vcos[p2i][i];
13203b705cfSriastradh		real mark_center;
13303b705cfSriastradh
13403b705cfSriastradh		if (clock < min_rate || clock > max_rate)
13503b705cfSriastradh			continue;
13603b705cfSriastradh
13703b705cfSriastradh		real xpos = scale_x(clock);
13803b705cfSriastradh		real vcofrac = (vco - min_vco) / (max_vco - min_vco);
13903b705cfSriastradh		real mark_height = (row_y1 + vcofrac * (row_y2 - row_y1));
14003b705cfSriastradh
14103b705cfSriastradh		move_to(cr, xpos, mark_height - 1);
14203b705cfSriastradh		line_to(cr, xpos, mark_height + 1);
14303b705cfSriastradh		stroke(cr);
14403b705cfSriastradh	}
14503b705cfSriastradh
14603b705cfSriastradh	set_source_rgb(cr, 0, 0, 0);
14703b705cfSriastradh	string p2label = sprintf("p2 = %d", p2);
14803b705cfSriastradh	move_to(cr, 5, (p2i + .5) / (dim(p2vals) + 1) * height + 4);
14903b705cfSriastradh	show_text(cr, p2label);
15003b705cfSriastradh}
15103b705cfSriastradh
15203b705cfSriastradhvoid label_clock(real clock) {
15303b705cfSriastradh	real center_x = scale_x(clock);
15403b705cfSriastradh	string label = sprintf("%d", floor((clock + 500) / 1000000));
15503b705cfSriastradh		text_extents_t e = text_extents(cr, label);
15603b705cfSriastradh	real left_x = center_x - e.x_advance / 2;
15703b705cfSriastradh	save(cr);
15803b705cfSriastradh	move_to(cr, left_x, height - 20);
15903b705cfSriastradh	show_text(cr, label);
16003b705cfSriastradh	restore(cr);
16103b705cfSriastradh}
16203b705cfSriastradh
16303b705cfSriastradhlabel_clock(min_rate);
16403b705cfSriastradhlabel_clock(max_rate);
16503b705cfSriastradhlabel_clock(140 * 1000 * 1000);
16603b705cfSriastradhlabel_clock(115 * 1000 * 1000);
16703b705cfSriastradhlabel_clock(100 * 1000 * 1000);
16803b705cfSriastradhlabel_clock(82 * 1000 * 1000);
16903b705cfSriastradh
17003b705cfSriastradhstring xlabel = "Clock in Mhz";
17103b705cfSriastradhtext_extents_t e = text_extents(cr, xlabel);
17203b705cfSriastradhmove_to(cr, width / 2 - e.x_advance / 2, height - 5);
17303b705cfSriastradhshow_text(cr, xlabel);
17403b705cfSriastradhsleep(10);
175