// File: mattdibi[P]Programmatore-JTAG-per-Xilinx/svftoserial.cpp
// Function: GenerateBITSTREAMOutput(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>)
vector<string> GenerateBITSTREAMOutput(vector<string> bitstream)
{
	vector<string> decBitstream;
	int indexRow, indexCol, initialCol;
	bool found = false;

	// Ignoriamo tutti i bit della SMASK
	indexRow  = bitstream.size() - 1;

	// Ciclo di ignore fino al carattere ')' che segnala la fine della SMASK
	while(indexRow >= 0 && found == false)
	{
		indexCol = bitstream[indexRow].length();

		while(indexCol >= 0 && found == false)
		{
			if(bitstream[indexRow][indexCol] == ')')
				found = true;

			indexCol--;
		}

		indexRow--;
	}

	indexRow++;

	// Il bitstream va inserito al contrario: dall'ultima riga alla prima
	for(int i = indexRow; i >= 0; i--)
	{
		string str = "";

		// Se è la prima riga dell'istruzione inseriamo la parte iniziale comune a tutte le istruzioni SDR
		// e partiamo con la copia dell'istruzione cominciando dalla posizione della ')'
		if(i == indexRow)
		{
			str += "!*";
			initialCol = indexCol;
		}
		else
		{
			// Se non è la prima riga partiamo dall'ultimo carattere della stringa
			initialCol = bitstream[i].length()-1;
		}

		for (int k = initialCol; k >= 0; k--)
		{
			if(i == 0 && k == 0)
			{
				string binval = hexCharToBin(bitstream[i][k]);

				for (i=binval.length()-1; i>0; i--)
				{
					if (binval[i]=='0') 
						str += ".";
					else if(binval[i]=='1') 
						str += ",";
				}

				if (binval[i]=='0') 
					str += ":";
				else if (binval[i]=='1') 
					str += ";";

				continue;
			}

			if(bitstream[i][k] != '\n' && bitstream[i][k] != '\r')
				str += bitstream[i][k];
		}

		// Se è l'ultima riga immetto la parte finale comune a tutte le istruzioni SDR
		if(i == 0)
			str+= "!*";

		decBitstream.push_back(str);
	}

	return decBitstream;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rsi, -0x88(%rbp)
movq %rdi, -0x98(%rbp)
movq %rdi, %rax
movq %rax, -0x90(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movb $0x0, -0x11(%rbp)
callq 0x5560
movq -0x88(%rbp), %rdi
movb $0x0, -0x21(%rbp)
callq 0x5640
subq $0x1, %rax
movl %eax, -0x18(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x18(%rbp)
movb %al, -0x99(%rbp)
jl 0x8fed
movb -0x21(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
sete %al
movb %al, -0x99(%rbp)
movb -0x99(%rbp), %al
testb $0x1, %al
jne 0x8ffc
jmp 0x90ac
movq -0x88(%rbp), %rdi
movslq -0x18(%rbp), %rsi
callq 0x5660
movq %rax, %rdi
callq 0x3410
movl %eax, -0x1c(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x1c(%rbp)
movb %al, -0x9a(%rbp)
jl 0x9039
movb -0x21(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
sete %al
movb %al, -0x9a(%rbp)
movb -0x9a(%rbp), %al
testb $0x1, %al
jne 0x9045
jmp 0x909e
movq -0x88(%rbp), %rdi
movslq -0x18(%rbp), %rsi
callq 0x5660
movq %rax, %rdi
movslq -0x1c(%rbp), %rsi
callq 0x3540
movq %rax, -0xa8(%rbp)
jmp 0x906a
movq -0xa8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x29, %eax
jne 0x9090
movb $0x1, -0x21(%rbp)
jmp 0x9090
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x9423
movl -0x1c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x9017
movl -0x18(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x8fcb
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
jl 0x93fd
leaq -0x59(%rbp), %rdi
movq %rdi, -0xb0(%rbp)
callq 0x34b0
movq -0xb0(%rbp), %rdx
leaq 0xf80(%rip), %rsi       # 0xa063
leaq -0x58(%rbp), %rdi
callq 0x54b0
jmp 0x90ee
leaq -0x59(%rbp), %rdi
callq 0x32e0
movl -0x38(%rbp), %eax
cmpl -0x18(%rbp), %eax
jne 0x9144
leaq 0x1692(%rip), %rsi      # 0xa798
leaq -0x58(%rbp), %rdi
callq 0x3270
jmp 0x9111
movl -0x1c(%rbp), %eax
movl %eax, -0x20(%rbp)
jmp 0x9163
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x59(%rbp), %rdi
callq 0x32e0
jmp 0x9423
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x93f2
movq -0x88(%rbp), %rdi
movslq -0x38(%rbp), %rsi
callq 0x5660
movq %rax, %rdi
callq 0x3410
subq $0x1, %rax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
movl %eax, -0x60(%rbp)
cmpl $0x0, -0x60(%rbp)
jl 0x93af
cmpl $0x0, -0x38(%rbp)
jne 0x92fb
cmpl $0x0, -0x60(%rbp)
jne 0x92fb
movq -0x88(%rbp), %rdi
movslq -0x38(%rbp), %rsi
callq 0x5660
movq %rax, %rdi
movslq -0x60(%rbp), %rsi
callq 0x3540
movq %rax, -0xb8(%rbp)
jmp 0x91ac
movq -0xb8(%rbp), %rax
movsbl (%rax), %esi
leaq -0x80(%rbp), %rdi
callq 0x8940
jmp 0x91c1
leaq -0x80(%rbp), %rdi
callq 0x3410
subq $0x1, %rax
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
jle 0x9279
movslq -0x38(%rbp), %rsi
leaq -0x80(%rbp), %rdi
callq 0x3540
movq %rax, -0xc0(%rbp)
jmp 0x91f1
movq -0xc0(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x30, %eax
jne 0x922e
leaq 0x133a(%rip), %rsi      # 0xa541
leaq -0x58(%rbp), %rdi
callq 0x3270
jmp 0x9212
jmp 0x9269
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x3190
jmp 0x93f2
movslq -0x38(%rbp), %rsi
leaq -0x80(%rbp), %rdi
callq 0x3540
movq %rax, -0xc8(%rbp)
jmp 0x9244
movq -0xc8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x31, %eax
jne 0x9267
leaq 0x152c(%rip), %rsi      # 0xa786
leaq -0x58(%rbp), %rdi
callq 0x3270
jmp 0x9265
jmp 0x9267
jmp 0x9269
jmp 0x926b
movl -0x38(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x91d1
movslq -0x38(%rbp), %rsi
leaq -0x80(%rbp), %rdi
callq 0x3540
movq %rax, -0xd0(%rbp)
jmp 0x928f
movq -0xd0(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x30, %eax
jne 0x92b2
leaq 0xe73(%rip), %rsi       # 0xa118
leaq -0x58(%rbp), %rdi
callq 0x3270
jmp 0x92b0
jmp 0x92ed
movslq -0x38(%rbp), %rsi
leaq -0x80(%rbp), %rdi
callq 0x3540
movq %rax, -0xd8(%rbp)
jmp 0x92c8
movq -0xd8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x31, %eax
jne 0x92eb
leaq 0x14c1(%rip), %rsi      # 0xa79f
leaq -0x58(%rbp), %rdi
callq 0x3270
jmp 0x92e9
jmp 0x92eb
jmp 0x92ed
leaq -0x80(%rbp), %rdi
callq 0x3190
jmp 0x93a1
movq -0x88(%rbp), %rdi
movslq -0x38(%rbp), %rsi
callq 0x5660
movq %rax, %rdi
movslq -0x60(%rbp), %rsi
callq 0x3540
movq %rax, -0xe0(%rbp)
jmp 0x9320
movq -0xe0(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xa, %eax
je 0x939f
movq -0x88(%rbp), %rdi
movslq -0x38(%rbp), %rsi
callq 0x5660
movq %rax, %rdi
movslq -0x60(%rbp), %rsi
callq 0x3540
movq %rax, -0xe8(%rbp)
jmp 0x9354
movq -0xe8(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xd, %eax
je 0x939f
movq -0x88(%rbp), %rdi
movslq -0x38(%rbp), %rsi
callq 0x5660
movq %rax, %rdi
movslq -0x60(%rbp), %rsi
callq 0x3540
movq %rax, -0xf0(%rbp)
jmp 0x9388
movq -0xf0(%rbp), %rax
movsbl (%rax), %esi
leaq -0x58(%rbp), %rdi
callq 0x31a0
jmp 0x939d
jmp 0x939f
jmp 0x93a1
movl -0x60(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x9169
cmpl $0x0, -0x38(%rbp)
jne 0x93c9
leaq 0x13dc(%rip), %rsi      # 0xa798
leaq -0x58(%rbp), %rdi
callq 0x3270
jmp 0x93c7
jmp 0x93c9
movq -0x98(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x5780
jmp 0x93db
leaq -0x58(%rbp), %rdi
callq 0x3190
movl -0x38(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x90bb
leaq -0x58(%rbp), %rdi
callq 0x3190
jmp 0x9423
movb $0x1, -0x11(%rbp)
testb $0x1, -0x11(%rbp)
jne 0x9413
movq -0x98(%rbp), %rdi
callq 0x55e0
movq -0x90(%rbp), %rax
addq $0xf0, %rsp
popq %rbp
retq
movq -0x98(%rbp), %rdi
callq 0x55e0
movq -0x30(%rbp), %rdi
callq 0x34a0