From 3210d68353d4bb3fa42dd3fa6966cc44c55323f5 Mon Sep 17 00:00:00 2001
From: "yu.dongliang" <18588496441@163.com>
Date: Tue, 17 Oct 2023 17:35:45 +0800
Subject: [PATCH] ses_layout_function()

---
 ses_layout.c | 59 +++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 52 insertions(+), 7 deletions(-)

diff --git a/ses_layout.c b/ses_layout.c
index 32224d5..c09d7f0 100644
--- a/ses_layout.c
+++ b/ses_layout.c
@@ -17,7 +17,6 @@ int epin_cmp(const void* v0, const void* v1)
 
 	if (t0[1] > t1[1])
 		return 1;
-
 	return 0;
 }
 
@@ -35,7 +34,6 @@ int epin_cmp_cx(const void* v0, const void* v1, void* arg)
 
 	if (c0->x > c1->x)
 		return 1;
-
 	return 0;
 }
 
@@ -49,7 +47,6 @@ int econn_cmp(const void* v0, const void* v1)
 
 	if (ec0->n_cids > ec1->n_cids)
 		return -1;
-
 	return 0;
 }
 
@@ -69,7 +66,6 @@ int eline_cmp(const void* v0, const void* v1)
 
 	if (el0->n_pins > el1->n_pins)
 		return -1;
-
 	return 0;
 }
 
@@ -83,7 +79,32 @@ int eline_cmp_id(const void* v0, const void* v1)
 
 	if (el0->id > el1->id)
 		return 1;
+	return 0;
+}
+
+int ecomponent_cmp_id(const void* v0, const void* v1)
+{
+	const ScfEcomponent* c0 = *(const ScfEcomponent**)v0;
+	const ScfEcomponent* c1 = *(const ScfEcomponent**)v1;
+
+	if (c0->id < c1->id)
+		return -1;
+
+	if (c0->id > c1->id)
+		return 1;
+	return 0;
+}
+
+int ecomponent_cmp_cx(const void* v0, const void* v1)
+{
+	const ScfEcomponent* c0 = *(const ScfEcomponent**)v0;
+	const ScfEcomponent* c1 = *(const ScfEcomponent**)v1;
 
+	if (c0->x < c1->x)
+		return -1;
+
+	if (c0->x > c1->x)
+		return 1;
 	return 0;
 }
 
@@ -483,8 +504,8 @@ static void __ses_layout_components(ScfEfunction* f, int d)
 				p0 = c->pins[el0->pins[m + 1]];
 				p1 = c->pins[el1->pins[n + 1]];
 
-				p0->x = el0->lines[0]->x1 + j * d + 10 + rand() % 20;
-				p1->x = el1->lines[0]->x1 + j * d + 10 + rand() % 20;
+				p0->x = el0->lines[0]->x1 + j * d + rand() % 10;
+				p1->x = el1->lines[0]->x1 + j * d + rand() % 10;
 				p0->y = el0->lines[0]->y0;
 				p1->y = el1->lines[0]->y0;
 
@@ -514,7 +535,7 @@ static void __ses_layout_components(ScfEfunction* f, int d)
 
 			if (0 == p0->x) {
 
-				p0->x = el0->lines[0]->x1 + d + 10 + rand() % 20;
+				p0->x = el0->lines[0]->x1 + d + rand() % 10;
 				p0->y = el0->lines[0]->y1;
 
 				el0->lines[0]->x1 = p0->x;
@@ -737,6 +758,7 @@ next:
 int ses_layout_function(ScfEfunction* f, int d)
 {
 	ScfEcomponent* c;
+	ScfEcomponent* c2;
 	ScfEline*      el;
 	ScfEline*      el2;
 	ScfEpin*       p0;
@@ -821,6 +843,29 @@ int ses_layout_function(ScfEfunction* f, int d)
 	int tx0 = INT_MAX;
 	int tx1 = 0;
 
+	qsort(f->components, f->n_components, sizeof(ScfEcomponent*), ecomponent_cmp_cx);
+
+	for (i = 1; i < f->n_components; i++) {
+		c  =        f->components[i];
+		c2 =        f->components[i - 1];
+
+		int tmp = c->x;
+
+		if (c->x > c2->x + d)
+			c->x = c2->x + d;
+
+		for (k = 0; k < c->n_pins; k++) {
+			p1 =        c->pins[k];
+
+			if (k < 2)
+				p1->x  = c->x;
+			else
+				p1->x += c->x - tmp;
+		}
+	}
+
+	qsort(f->components, f->n_components, sizeof(ScfEcomponent*), ecomponent_cmp_id);
+
 	for (i = 0; i < f->n_elines; i++) {
 		el        = f->elines[i];
 
-- 
2.25.1