aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPepijn de Vos <pepijndevos@gmail.com>2019-10-16 05:43:03 +0200
committertgingold <tgingold@users.noreply.github.com>2019-10-16 05:43:03 +0200
commit0b29a7cb792bd07b112671a264defcb1085ba402 (patch)
tree3fc8c9486884676b7f55f6a95c57774c696e1442 /src
parent96ceed2f296a44ca60fc8cd0f91c35edcc2d7b41 (diff)
downloadghdl-yosys-plugin-0b29a7cb792bd07b112671a264defcb1085ba402.tar.gz
ghdl-yosys-plugin-0b29a7cb792bd07b112671a264defcb1085ba402.tar.bz2
ghdl-yosys-plugin-0b29a7cb792bd07b112671a264defcb1085ba402.zip
Sign extend 32b literals (#61)
* sign extend 32b literals * Fix undefined behavior Right shift of a signed values is undefined but does arithemetic shift in practice. However, shifting by more than one int width is also undefined but *wraps around*. This caused bit/log to work because it'd shift mod 32. But it actually cause the UL32 to be wrong because it'd just repeat the value rather than extending. * zero pad unsigned and add signed * add testsuite
Diffstat (limited to 'src')
-rw-r--r--src/ghdl.cc55
1 files changed, 51 insertions, 4 deletions
diff --git a/src/ghdl.cc b/src/ghdl.cc
index 8d6a287..d6c3cba 100644
--- a/src/ghdl.cc
+++ b/src/ghdl.cc
@@ -87,8 +87,7 @@ static RTLIL::SigSpec get_src(std::vector<RTLIL::Wire *> &net_map, Net n)
RTLIL::SigSpec res = IN(0);
return res.extract(0, get_width(n));
}
- case Id_Const_Bit:
- case Id_Const_UB32:
+ case Id_Const_Bit: // arbitrary width binary
{
const unsigned wd = get_width(n);
std::vector<RTLIL::State> bits(wd);
@@ -96,10 +95,31 @@ static RTLIL::SigSpec get_src(std::vector<RTLIL::Wire *> &net_map, Net n)
for (unsigned i = 0; i < wd; i++) {
if (i % 32 == 0)
val = get_param_uns32(inst, i / 32);
+ bits[i] = (val >> (i%32)) & 1 ? RTLIL::State::S1 : RTLIL::State::S0;
+ }
+ return RTLIL::SigSpec(RTLIL::Const(bits));
+ }
+ case Id_Const_UB32: // zero padded binary
+ {
+ const unsigned wd = get_width(n);
+ std::vector<RTLIL::State> bits(wd);
+ unsigned int val = get_param_uns32(inst, 0);
+ for (unsigned i = 0; i < wd && i < 32; i++) {
bits[i] = (val >> i) & 1 ? RTLIL::State::S1 : RTLIL::State::S0;
}
return RTLIL::SigSpec(RTLIL::Const(bits));
}
+ case Id_Const_SB32: // sign extended binary
+ {
+ const unsigned wd = get_width(n);
+ std::vector<RTLIL::State> bits(wd);
+ unsigned int val = get_param_uns32(inst, 0);
+ for (unsigned i = 0; i < wd; i++) {
+ unsigned idx = i < 32 ? i : 31;
+ bits[i] = (val >> idx) & 1 ? RTLIL::State::S1 : RTLIL::State::S0;
+ }
+ return RTLIL::SigSpec(RTLIL::Const(bits));
+ }
case Id_Const_Z:
{
return SigSpec(RTLIL::State::Sz, get_width(n));
@@ -108,8 +128,7 @@ static RTLIL::SigSpec get_src(std::vector<RTLIL::Wire *> &net_map, Net n)
{
return SigSpec(RTLIL::State::Sx, get_width(n));
}
- case Id_Const_Log:
- case Id_Const_UL32:
+ case Id_Const_Log: // arbitrary lenght 01ZX
{
const unsigned wd = get_width(n);
std::vector<RTLIL::State> bits(wd);
@@ -120,6 +139,32 @@ static RTLIL::SigSpec get_src(std::vector<RTLIL::Wire *> &net_map, Net n)
val01 = get_param_uns32(inst, 2*(i / 32));
valzx = get_param_uns32(inst, 2*(i / 32) + 1);
}
+ switch(((val01 >> (i%32))&1)+((valzx >> (i%32))&1)*2)
+ {
+ case 0:
+ bits[i] = RTLIL::State::S0;
+ break;
+ case 1:
+ bits[i] = RTLIL::State::S1;
+ break;
+ case 2:
+ bits[i] = RTLIL::State::Sz;
+ break;
+ case 3:
+ bits[i] = RTLIL::State::Sx;
+ break;
+ }
+
+ }
+ return RTLIL::SigSpec(RTLIL::Const(bits));
+ }
+ case Id_Const_UL32: // zero padded 01ZX
+ {
+ const unsigned wd = get_width(n);
+ std::vector<RTLIL::State> bits(wd);
+ unsigned int val01 = get_param_uns32(inst, 0);
+ unsigned int valzx = get_param_uns32(inst, 0);
+ for (unsigned i = 0; i < wd && i < 32; i++) {
switch(((val01 >> i)&1)+((valzx >> i)&1)*2)
{
case 0:
@@ -328,6 +373,7 @@ static void import_module(RTLIL::Design *design, GhdlSynth::Module m)
case Id_Output:
case Id_Port:
case Id_Const_UB32:
+ case Id_Const_SB32:
case Id_Const_UL32:
case Id_Const_Bit:
case Id_Const_Log:
@@ -523,6 +569,7 @@ static void import_module(RTLIL::Design *design, GhdlSynth::Module m)
module->addCover(to_str(iname), IN(0), State::S1);
break;
case Id_Const_UB32:
+ case Id_Const_SB32:
case Id_Const_UL32:
case Id_Const_Bit:
case Id_Const_Log:
n314' href='#n314'>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